我可以想象以下代码:
template <typename T> class X
{
public:
T container;
void foo()
{
if(is_vector(T))
container.push_back(Z);
else
container.insert(Z);
}
}
// somewhere else...
X<std::vector<sth>> abc;
abc.foo();
Run Code Online (Sandbox Code Playgroud)
如何编写,成功编译?我知道类型特征,但是当我定义时:
template<typename T> struct is_vector : public std::false_type {};
template<typename T, typename A>
struct is_vector<std::vector<T, A>> : public std::true_type {};
Run Code Online (Sandbox Code Playgroud)
它不编译:
error: no matching function for call to 'std::vector<sth>::insert(Z)'
Run Code Online (Sandbox Code Playgroud)
static_assert也不是我想要的.有什么建议吗?
这是我想要实现的一个简短例子(SSCCE):http://ideone.com/D3vBph