For study purpose, I make own Any
type. I don't understand, how to return Type
or Type *
depending on the conditions. This is my sketch of class:
class any
{
public:
template<class T>
any(T & d)
{
data_container = new container_impl<T>(d);
}
template<class T>
any(T* d)
{
is_pointer = true;
data_container = new container_impl<T>(d);
}
bool check_is_pointer() const;
template<class T>
T a_cast(size_t id) const
{
auto real_id = data_container->get_id();
if (real_id != id)
{
//throw new exeption
}
return static_cast<container_impl<T>&>(*data_container).get_data();
}
private:
class abstract_container
{
public:
virtual ~abstract_container() { }
virtual size_t get_id() const = 0;
};
template<typename T>
class container_impl : public abstract_container
{
public:
container_impl(T const& value)
{
data = value;
id = type_id<T>();
pointer_data = nullptr;
}
container_impl(T const* value)
{
pointer_data = value;
id = type_id<T*>();
}
T get_data()
{
return data;
}
size_t get_id() const override
{
return id;
}
private:
T data;
T const* pointer_data;
size_t id;
};
abstract_container *data_container = nullptr;
bool is_pointer = false;
};
template<class T>
T any_cast(any const& obj)
{
size_t id = type_id<T>();
return obj.a_cast<T>(id);
}
template<class T>
T* any_cast(any const& obj)
{
size_t id = type_id<T>();
return obj.a_cast<T>(id);
}
As you can see, I would like to create two functions any_cast
, which return different types.
You can't return different types. The way that Boost does it is actually with different signatures - note that the two different versions take different arguments. One takes a reference and the other takes a pointer.
Example usage of the two different functions: