开发者

Explicit specialization template member and typedef

Found a curious emergent architecture (it wasn't designed intentionally but was created over time by adding up features) in code base, which looks like this, if dumbed down:

#include <iostream>
// Base class
template <class T>
struct Base {
   typedef T DataT;
   void foo();
};

// Parameter classes
struct A { int a[2]; };
struct B { int a[3]; };

// In original code Base<> had a complex list of arguments
typedef Base<A> ContainerA;
typedef Base<B> ContainerB;

// what allows to use ContainerA? It's a type name or explicit specialization?
// Class parameters, e.g. T, are unusable in this context  directly.
template<> void ContainerA::foo() {
    std::cout << "ContainerA " << sizeof(DataT) << std::endl;
}

template<> void ContainerB::foo() {
    std::cout << "ContainerB " << sizeof(DataT) << std::endl;
}

int main()
{
    ContainerA a;
    ContainerB b;
    a.foo();
    b.foo();
}

Essentially, with multiple derived classes and complex template argument list this use of typedef saves time to type code and makes specialization declarations more readable. But which part of standard says th开发者_开发百科at a typedef can be used in such way?

Code had partial support of C++11, no using = type declarations allowed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜