【发布时间】:2015-11-20 13:41:30
【问题描述】:
给定一个容器,例如std::list<T>或std::vector<T>,我想在我不知道容器(std::list或std::vector)的情况下分别生成一个新类型std::list<NewT>或std::vector<NewT> ) 提前。
我当前(但 failing)的尝试如下所示:
#include <list>
#include <vector>
template<class Cont, class NewT> struct SameContNewT : public std::false_type
{};
template<class T, class Alloc, class NewT>
struct SameContNewT<std::list<T, Alloc>, NewT>
{ typedef typename std::list<NewT> type; };
template<class T, class Alloc, class NewT>
struct SameContNewT<std::vector<T, Alloc>, NewT>
{ typedef typename std::vector<NewT> type; };
int main()
{
typedef std::list<int> IntList;
typedef std::list<float> FloatList;
typedef SameContNewT<IntList, float> FloatListToo;
static_assert(std::is_same<FloatListToo, FloatList>::value, "No.");
}
如何实现预期的行为?
【问题讨论】:
标签: c++ templates c++11 c++14 template-meta-programming