如何正确编写模板模板参数?
How do I write template template parameters correctly?
我正在尝试使用模板为任何c++集合制作适配器。
我需要使用模板模板参数,所以我可以使用适配器:
CollectionAdapter<std::vector<int>> a;
而不是:
CollectionAdapter<std::vector<int>,int> a;
一次需要两个模板形参。
我写了这个类:
template <
template <class U> class T
>
class CollectionAdapter {
public:
typedef T<U> ThisCol;
typedef void iterator;
CollectionAdapter() {}
bool add(ThisCol& c,const U& i);
bool remove(ThisCol& c,const U& i);
U& getByIndex(int i);
ThisCol instantiate();
iterator getIterator(ThisCol& c);
};
但是,visual studio编译器给我这个错误:
error C2065: 'U' : undeclared identifier
对于这行:
typedef T<U> ThisCol;
我做错了什么?
我认为你不需要模板模板参数。你可以简化你的代码:
template <class T>
class CollectionAdapter
{
public:
typedef T ThisCol;
typedef typename T::value_type value_type;
//typedef void iterator; // what?? did you mean void*?
typedef void* void_iterator; // but not sure what the use of this is.
// you might need the container's iterator types too
typedef typename T::iterator iterator
typedef typename T::const_iterator const_iterator
CollectionAdapter() {}
bool add(T& c,const value_type& i);
bool remove(T& c,const value_type& i);
value_type& getByIndex(int i);
const value_type& getByIndex(int i) const;
ThisCol instantiate();
iterator getIterator(T& c);
};
相关文章:
- 失败,出现错误 87:参数不正确.如何判断哪个参数不正确?
- CreateThread的参数不正确
- Boost::Asio串行读/写打开:参数不正确
- 模板对象管理器,参数不正确
- 收到错误 87,安装程序的参数不正确DiGetDeviceInterfaceDetail
- 在UWP页面之间传递参数的正确方法
- C++ 在函数参数中正确使用结构
- C++ 定义函数默认参数的正确方法
- std ::旋转参数不正确
- 什么是“std::nullptr_t”模板参数的正确用例
- R 值引用参数的正确默认值是什么?
- 为什么对齐的参数编译正确,但在运行时崩溃
- 字符星形数组参数未正确终止
- 'decltype'参数不正确
- libcurl curl_easy_stopt long参数未正确传递-varg(param,long)
- LoadLibraryEx错误87(参数不正确)
- 如何在 C++11 中使用可变参数模板正确创建显式参数转发函数
- Win32 窗口包装器出错(参数不正确)
- 如何检查类中传递的参数是否正确
- 参数不正确