我无法理解这个C ++模板是如何工作的

时间:2015-08-14 01:47:16

标签: c++ templates template-templates

我正在阅读图书馆的源代码。但我无法理解这个模板定义的工作原理:

template <class A>
struct Use{
    template <class T> struct AsVertexType: public T{typedef A VertexType;  typedef VertexType * VertexPointer  ;};
    template <class T> struct AsEdgeType: public T{typedef A EdgeType;          typedef EdgeType *  EdgePointer         ;};
    template <class T> struct AsFaceType: public T{typedef A FaceType;          typedef FaceType * FacePointer          ;};
    template <class T> struct AsTetraType: public T{typedef A TetraType;        typedef TetraType * TetraPointer        ;};
    template <class T> struct AsHEdgeType: public T{typedef A HEdgeType;        typedef HEdgeType * HEdgePointer        ;};
};

这就是如何使用Use&lt;&gt;声明一个类模板:

struct MyUsedTypes : public vcg::UsedTypes< vcg::Use<MyVertex> :: AsVertexType,
                                            vcg::Use<MyEdge>   :: AsEdgeType,
                                            vcg::Use<MyFace>   :: AsFaceType> {};

在使用&lt;&gt;模板定义,有两种未知的类型A和T.

vcg::Use<MyVertex>::AsVertexType为例,我认为A是MyVertex,但是什么是T?

补编

这是模板UsedTypes的定义

template < template <typename> class A = DefaultDeriver, template <typename> class B = DefaultDeriver,
           template <typename> class C = DefaultDeriver, template <typename> class D = DefaultDeriver,
           template <typename> class E = DefaultDeriver, template <typename> class F = DefaultDeriver,
           template <typename> class G = DefaultDeriver, template <typename> class H = DefaultDeriver >
class UsedTypes
    : public Arity12<DummyTypes,
                     Use< Vertex< UsedTypes<A, B, C, D , E, F, G, H> > > :: template AsVertexType,
                     Use< Edge  < UsedTypes<A, B, C, D , E, F, G, H> > > :: template AsEdgeType,
                     Use< Face  < UsedTypes<A, B, C, D , E, F, G, H> > > :: template AsFaceType,
                     Use< HEdge < UsedTypes<A, B, C, D , E, F, G, H> > > :: template AsHEdgeType,
                     A, B, C, D, E, F, G, H > {};

和一系列Arity模板定义。这就像数学归纳

template < class Base,
           template <typename> class A >
class Arity1: public A<Base> {};


template < class Base,
           template <typename> class A,
           template <typename> class B > 
class Arity2: public B<Arity1<Base, A>> {};


template < class Base,
           template <typename> class A, template <typename> class B,
           template <typename> class C > 
class Arity3: public C<Arity2<Base, A, B>> {};
   .
   .
   .
template < class Base,
           template <typename> class A, template <typename> class B,
           template <typename> class C, template <typename> class D,
           template <typename> class E, template <typename> class F,
           template <typename> class G, template <typename> class H,
           template <typename> class I, template <typename> class J,
           template <typename> class K, template <typename> class L >
class Arity12: public L<Arity11<Base, A, B, C, D, E, F, G, H, I, J, K>> {};

仍然很难说模板参数T是什么。

2 个答案:

答案 0 :(得分:1)

vcg::Use<MyVertex> ::AsVertexType是一个模板,当作为模板参数提供给vcg::UsedTypes时,构成模板模板参数 - 如果您搜索该术语,那么您将会这样做找到很多解释。由vcg::UsedTypes模板决定是否使用AsVertexType模板以及T个参数。

答案 1 :(得分:0)

看起来vcg::UsedTypes将模板作为模板参数。因此TUsedTypes使用模板的任何类型。