How to return different types from same function?

435 views Asked by At

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.

1

There are 1 answers

0
Barry On BEST ANSWER

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.

 template <typename T>   // this one will try to cast your any down to T
 T any_cast(any& );      // and return it if that succeeds, else throw

 template <typename T>   // this one will NOT throw, but will return
 T* any_cast(any* );     // a pointer instead

Example usage of the two different functions:

any a = 5;
any_cast<double>(a); // throws

double* v = any_cast<double>(&a); // won't throw
assert(v == nullptr);

int* vi = any_cast<int>(&a); // succeeds
assert(*vi == 5);