Hold any kind of C++ template class in member variable

孤人 提交于 2019-11-30 20:27:50

You cannot have a single class B with "any" member object, because B has to be a well-defined class, and A<T> is a different type for different types T. You can either make B a template itself:

template <typename T>
class B
{
  A<T> value;
};

or you can take a look at boost::any, which is type-erasing container for arbitrary types (but making use of it requires a certain amount of extra work). The any class only works for value types, though, it's not completely arbitrary.

The simplest solution would be to make all A variants ineherit from a common interface, even if it's empty :

class IA{}

template <class T>
class A : public IA
{
    public:
        T value;
};

class B
{
    public:
        IA* value;
};

Now, the associated costs:

  • interactions with value are limited to the IA interface;
  • if you try to cast to get the real type, that mean that you know the real type, so it's of no use and make A type a parameter of B becomes really easier to use.
  • there are runtime costs associated to runtime inheritance

Advantage :

  • it's easily understood by other developers
  • it naturally limit the types possible to some specific ones
  • it don't use boost (sometimes, you just can't)

So to do better there are other less simple solutions but that are simple enough to be used :

If you can use boost, boost::any, boost::variant and boost::mpl might be base of solutions.

Boost any can be used as a safe replacement to void*. The only problem with this is that you can have ANY type, like if the type was a template parameter of the B class.

Boost variant might be used successfully if you know all the types that A can be.

MPL might be helpful if you just want to set a list of possible types and make sure your members apply only to them. You can do a ton of things with MPL so it really depends on your exact needs.

You've got two choices, I think. The first is to parameterize your class over the type parameters of the instance variables:

template <class T> struct B
{
  A<T> value;
};

The other option is to declare value as a void* pointer. (But that's probably not what you want).

yes, it's already been done. boost::any.

I think it helps to understand, that templated classes create an entirely new and seperate class for every type you use with it. For instance, Vector<int> and Vector<float> are as separate as the classes VectorInt and VectorFloat.

For class B, you are basically asking that the value variable either be A<int> or A<float>, which is the same as saying you want value to either be a "A_int" or "A_float". And to accomplish that you... well, use another template!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!