4

当我尝试为通用容器(例如std::list<...>,而不是特定容器,例如std::list<double>)专门化模板变量时,我得到一个链接错误gcc 5.3(但不是clang 3.5

/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 = "->";
4

1 回答 1

1

这似乎是一个问题gcc。解决方法(使用类模板),如建议的@TC。

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;

这适用于gccclang


或者@TC也建议,使用静态函数成员而不是静态成员(更少的代码)

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),但我遇到了奇怪的链接器错误。

于 2016-01-27T04:00:51.303 回答