相关疑难解决方法(0)

在编译时检查是模板类型向量

我可以想象以下代码:

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

c++ templates

19
推荐指数
3
解决办法
3842
查看次数

标签 统计

c++ ×1

templates ×1