template <bool A=true,bool B=false>struct from { const static std::string value; };// no specialization - workstemplate <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 argumentstemplate <bool B>const std::string from<true,B>::value = "";// full specialization - worksconst 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; };
同样,问题是为什么?
解决方法 如果没有封闭类模板本身的部分特化,则不允许成员的部分特化(无论它们是函数还是静态数据).也就是说,您还必须专门化类模板.所以以下应该有效:
//partial specialization of class templatetemplate <bool B>struct from<true,B> { const static std::string value; };//Now you can do this! template <bool B>const std::string from<true,B>::value = ""
此外,这将无法编译(你试过编译吗?):
// full specialization - works (SORRY,IT WILL NOT WORK!)const std::string from<false,true>::value = ""; //this should be an error
你要写这个:
// full specialization template<> //<---- this is important!const std::string from<false,true>::value = ""总结
以上是内存溢出为你收集整理的c如何初始化部分模板特化的静态变量全部内容,希望文章能够帮你解决c如何初始化部分模板特化的静态变量所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)