samedi 27 février 2016

Preserving the type and its value

Here is the code that I am working on.

class HoldThis
{
public:
    template<typename T> explicit 
    HoldThis(const T& in)
    :a_in(in)
    ,a_givemebase([=]()-> const SomeBaseType& { 
        return boost::any_cast<const T&>(a_in); })
    { }

    const SomeBaseType& getBase() const
    {
        return a_givemebase();
    }

private:
    boost::any a_in;
    std::function<const SomeBaseType& ()> a_givemebase;
};

The problem is that type T that comes in is always derived from SomeBaseType. I want to preserve T and the value in that comes with it via some form of template metaprogramming.

Is this possible? To preserve T and then return its value via a getter when required. I am using C++11 and I can not use any C++14 or beyond features.

I have tried using boost::mpl::identity to fetch the type T but someway or other, I am stuck with not having the knowledge of T or the value that comes in.

Aucun commentaire:

Enregistrer un commentaire