大佬教程收集整理的这篇文章主要介绍了c – 模板变量的专业化(用于模板模板类),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
/tmp/ccvxFv3R.s: Assembler messages: /tmp/ccvxFv3R.s:206: Error: symbol `_ZL9separator' is already defined
http://coliru.stacked-crooked.com/a/38f68c782d385bac
#include<string> #include<iostream> #include<list> #include<forward_list> #include<vector> template<typename T> std::string const separator = ","; template<typename... Ts> std::string const separator<std::list<Ts...> > = "<->"; template<typename... Ts> std::string const separator<std::forward_list<Ts...>> = "->"; int main(){ std::cout << separator<std::vector<double>> << '\n'; std::cout << separator<std::list<double>> << '\n'; std::cout << separator<std::forward_list<double>> << '\n'; }
(这与clang 3.5很好地编译并且按预期工作.此外,可变参数模板不是导致问题的原因,我尝试使用非可变参数模板).
如果这不是gcc中的错误,你认为有没有解决方法?我试图使用类专门化,但它也不可能:
template<class T> struct separator{ static std::string const value; }; template<class T> std::string const separator<T>::value = ","; template<typename... Ts> std::string const separator<std::list<Ts...>>::value = "<->"; template<typename... Ts> std::string const sep<std::forward_list<Ts...>>::value = "->";
template<class T> struct sep{ static const std::string value; }; template<class T> const std::string sep<T>::value = ","; template<typename... Ts> struct sep<std::list<Ts...>>{ static const std::string value; }; template<typename... Ts> const std::string sep<std::list<Ts...>>::value = "<->"; template<typename... Ts> struct sep<std::forward_list<Ts...>>{ static const std::string value; }; template<typename... Ts> const std::string sep<std::forward_list<Ts...>>::value = "->";
以后是模板变量(所以具有相同的接口)
template<typename T> std::string const separator = sep<T>::value;
这适用于gcc和clang.
或者@ T.C.建议使用静态函数成员而不是静态成员(代码少)
template<class T> struct sep{ static std::string value(){return ",";} }; template<typename... Ts> struct sep<std::list<Ts...>>{ static std::string value(){return "<->";} }; template<typename... Ts> struct sep<std::forward_list<Ts...>>{ static std::string value(){return "->";} }; ... template<typename T> std::string const separator = sep<T>::value();
或者使用constexpr const char *
template<class T> struct sep{static constexpr const char* value = ",";}; template<typename... Ts> struct sep<std::list<Ts...>>{static constexpr const char* value = "<->";}; template<typename... Ts> struct sep<std::forward_list<Ts...>>{static constexpr const char* value = "->";}; ... template<typename T> std::string const separator = sep<T>::value;
我试图使用const_str(一个constexpr友好版本的std :: string),但我得到了奇怪的链接器错误.
以上是大佬教程为你收集整理的c – 模板变量的专业化(用于模板模板类)全部内容,希望文章能够帮你解决c – 模板变量的专业化(用于模板模板类)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。