模板typename为同一模板的其他参数的类型
Template typename as type of other parameter of the same template
我想创建一个模板,在那里我可以使用不同的矢量类型,和一个常量作为相同类型的矢量。我希望能够传递常量作为模板参数,以便让编译器优化它,而不需要显式特化。
所以我尝试重新使用类型参数,但我得到了非法的类型。
#include <vector>
using namespace std;
template<typename WEIGHT, WEIGHT multiplier>
void test_multipier(vector<WEIGHT> &v)
{
uint16_t num = 16;
/*...*/
WEIGHT w = multiplier* (num);
v.push_back(w);
}
int main()
{
vector<double> test_vector;
test_multipier<double,0.01>(test_vector);
//^^ Error C2993 'double': illegal type for non-type template parameter 'multiplier'
return 0;
}
摘自cppreference.com:
[非类型模板形参的类型]是以下类型之一(可选的cv限定,限定符将被忽略):
std::nullptr_t
(c++ 11起);- 积分类型;
- 左值引用类型(对象或函数);
- 指针类型(指向对象或函数);
- 指向成员类型(指向成员对象或成员函数)的指针;
- 枚举类型。
double
不能用作非类型模板参数
相关文章:
- C++ 默认参数使用其他参数
- lambda 作为接受其他参数的参数的初始化顺序
- 了解'this'或其他参数是否为右值
- 递归回文检查,不使用向量、大小或其他参数
- 模板模板参数用作其他参数的默认值
- 可以移动构造函数以除班级本身以外的其他参数
- 在传递其他参数时在 C++ 中重载运算符
- 具有可变参数模板参数的部分专用化,后跟其他参数
- 在参数包中获取函数指针的返回类型,并将其保存为与其他参数连接的元组
- 委托构造函数和默认参数,具体取决于其他参数
- 在boost :: static_visitor中operator()中的其他参数
- 带有映射参数的函数需要其他参数
- 是否有一个WinAPI可以从带有可选空格和其他参数的命令行获取文件名
- 后面跟有其他参数的函数参数包的模板参数推导
- 将字符串(或任何其他参数)传递到QWidget构造函数
- 使用其他参数的默认参数
- 如何设置依赖于其他参数的参数默认值
- C++获取具有其他参数的构造函数的初始值设定项列表
- C++函数模板,函数作为参数,取决于其他参数
- 使用 boost::bind 但允许传递任何其他参数