我正在阅读图书馆的源代码。但我无法理解这个模板定义的工作原理:
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是什么。
答案 0 :(得分:1)
vcg::Use<MyVertex> ::AsVertexType
是一个模板,当作为模板参数提供给vcg::UsedTypes
时,构成模板模板参数 - 如果您搜索该术语,那么您将会这样做找到很多解释。由vcg::UsedTypes
模板决定是否使用AsVertexType
模板以及T
个参数。
答案 1 :(得分:0)
看起来vcg::UsedTypes
将模板作为模板参数。因此T
将UsedTypes
使用模板的任何类型。