【发布时间】:2012-11-15 19:26:30
【问题描述】:
我应该如何为偏特化初始化一个静态变量?
template <bool A=true, bool B=false>
struct from {
const static std::string value;
};
// no specialization - works
template <bool A, bool B>
const std::string from<A, B>::value = "";
// partial specialization - does not compile -
// Error: template argument list following class template name must list parameters in the order used in template parameter list
// Error: from<A,B>' : too few template arguments
template <bool B>
const std::string from<true, B>::value = "";
// full specialization - works
const std::string from<false, true>::value = "";
为什么部分不起作用?
编辑:我找到了基于Partial template specialization for initialization of static data members of template classes的解决方案
在允许我初始化静态变量之前,我需要重复部分特化的声明:
template <bool B>
struct from<true, B> {
const static std::string value;
};
再次,问题是为什么?
【问题讨论】:
-
哪个编译器?在g++ 4.3.4,最后一个也不起作用。
-
你不需要专门化整个类模板吗?我认为成员只允许显式(=完全)专业化。
标签: c++ templates template-specialization