开发者

how to end-up template layers?

When having:

template <typename Super>
class Whatever : public Super
{
    ...
};

is it possible, to create Whatever class without deriving from something?

Is this the lighter version?

struct BlankType{};

Whatever<BlankType> w;

////////////////////////////////////////

Some background:

I have my code composed into template layers like Whatever above. So I can do开发者_开发技巧:

typedef Whatever<Whenever<Wherever<>>>> MyCombinedType

actually I can not. I have to do

typedef Whatever<Whenever<Wherever<BlankType>>>> MyCombinedType

and the type becomes also BlankType. I can not make Wherever "non-layerable", because when I would do just

typedef Whatever<Whenever<>>> MyCombinedType

the problem will appear again...


If you want to create Whatever class that is not derived from something you can simply define its specification as follows:

class BlankType {};
template<typename T = BlankType> class Whatever : public T {};
template<> class Whatever<BlankType> {};


A bit off-topic, in C++ with variadic templates you can avoid the recursive instantiation thanks to a recursive definition:

template <class ...Bases> class Whatever;

template <class B, class ...Bases>
class Whatever<B, Bases...> : public B, public Whatever<Bases...> { /* ... */ };

template <class B>
class Whatever<B> : public B { /*... */ };

template <> class Whatever<> { /* ... */ };

Now you can say Whatever<Foo, Bar, Baz> and inherit from all those. If you want to inherit also from multiply nested other instances of Whatever, you should make all the inheritances virtual.

The final specialization in my example also shows how you can specialize Whatever to not derive from anything. If you write Whatever<> x;, you have an object of a class that does not derive from anything.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜