模板模板参数不起作用
template template parameters not working
我有以下类:
template <class T, size_t dims> class dataType { ... };
class heapType {...}; // Not templated so far.
class dataClass {....};
这两个类工作正常。现在我想做以下事情:
template < template <class T, size_t dims> class data_t, class heap_t> algorithmType {...};
目标是将algorithmType
与其他类一起用作策略:
algorithmType <dataType<dataClass,2>, heapType> myAlgorithm;
但这引发了以下错误:
error: ‘myAlgorithm’ was not declared in this scope
algorithmType <dataType<dataClass,2>, heapType> myAlgorithm;
^
error: expected primary-expression before ‘,’ token
algorithmType <dataType<dataClass,2>, heapType> myAlgorithm;
^
error: expected primary-expression before ‘>’ token
algorithmType <dataType<dataClass,2>, heapType> myAlgorithm;
^
error: ‘myAlgorithm’ was not declared in this scope
algorithmType <dataType<dataClass,2>, heapType> myAlgorithm;
我正在阅读《现代C++》一书的第一章,其中也有类似的内容。问题出在哪里?非常感谢。
您声明了一个模板模板参数,但在中传递了一个类型,而不是模板更改用途并传入模板:
algorithmType <dataType, heapType> myAlgorithm;
或者更改声明以接受类型:
template < class data_t, class heap_t> algorithmType {...};
哪一个取决于您的预期用途。
如果algorithmType
只想传入一个类,请将其更改为具有模板类型的参数,然后像现在这样传入类dataType<dataClass,2>
。类是从模板实例化的并不重要,它是一个类
另一方面,如果您希望algorithmType
能够为其输入模板参数提供自己的模板参数,请将其保留为模板模板参数,然后将模板传入。
问题是algorithmType
需要一个接受type和int作为第一个参数的模板类,而您正在传递一个具体类型。
简单地说,这是定义变量的正确方法:
algorithmType <dataType, heapType> myAlgorithm;
从注释中,我发现您不理解模板类作为模板参数的含义。
下一个例子编译并展示了如何使用模板类作为模板参数:
#include <list>
#include <vector>
template< template < class, class > class V >
struct A
{
V< int, std::allocator<int> > container;
};
int main()
{
A< std::vector > a1; // container is std::vector<int>
A< std::list > a2; // container is std::map<int>
}
如果你需要传递一个具体的类型,那么将algorithmType更改为:
template class data_t, class heap_t> algorithmType {...};
相关文章:
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- 我正在开发服务器,ip作为参数传递不起作用
- G++ C++17 类模板参数推导在非常特殊的情况下不起作用
- 类中的虚拟布尔函数参数不起作用
- 模板 ctor 类型推导不起作用(没有匹配的构造函数用于初始化 ...)与函数<>参数
- 我的代码在作为参数传入 .begin() 时不起作用,但在我将 .begin() 转换为迭代器后工作
- 为什么重载运算符>在参数声明 const 时不起作用?
- 为什么当我提供一些模板参数时,C++17 模板类推导不起作用?
- 为什么static_assert在带有 const 参数的成员函数中不起作用?
- ConvertToReferencePoint 方法不起作用,获取作业参数的 NULL (HyperV 2016)
- 线程函数参数的前向声明不起作用
- 模板参数类型推导在函数对象中不起作用
- 将参考类型作为嵌套模板结构中的模板参数作为模板参数不起作用
- 为什么参数推断在此模板模板参数中不起作用
- C++ 数组参数不起作用
- 带有 3 个参数的冲刺不起作用
- 模板参数在具有相同数据类型的单个类型名的构造函数中不起作用
- 像在 Python 中一样C++循环中的参数解析。为什么不起作用?
- C 类是功能不起作用的参数
- 为什么在参数输入之前将计算放置在代码中时,计算不起作用