【发布时间】:2011-03-24 13:09:13
【问题描述】:
考虑下一个例子:
#include <iostream>
#include <typeinfo>
template< int N, typename T >
struct B
{
struct C;
};
template< typename T >
struct B< 0, T >::C
{
typedef T type;
};
template< int N, typename T >
struct B< N, T >::C
{
typedef T type[N];
};
int main()
{
std::cout<<"n=0 type = " << typeid( B< 0, float >::C::type ).name() << std::endl;
std::cout<<"n=5 type = " << typeid( B< 5, float >::C::type ).name() << std::endl;
}
使用 g++(4.3.0 版)编译时
g++ dfg.cpp -ansi -pedantic -Wall
编译错误是:
dfg.cpp:13: error: qualified name does not name a class before ‘{’ token
dfg.cpp: In instantiation of ‘B<0, float>::C’:
dfg.cpp:25: instantiated from here
dfg.cpp:20: error: ISO C++ forbids zero-size array
我真正想要归档的是根据枚举值有不同的 Imp 实现(在示例中,我使用 int 而不是枚举,但这不重要)。
有人可以解释为什么不允许这样做吗? 为什么我会收到第一个错误? (这个:限定名没有在‘{’标记之前命名一个类)
关于取决于模板参数的 pimpl 实现,我创建了一个新问题(有更好的例子)here
【问题讨论】:
标签: c++ templates metaprogramming partial-specialization pimpl-idiom