嵌套的C 模板
Nested C++ templates
i具有一个称为 add_vector_to_scalar
的函数,该函数将标量值添加到向量(in
),并将结果存储在另一个向量(out
)中。我正在学习C ,所以我不确定如何将add_op generic
的类型参数制作?我考虑过添加另一个打字机T
,但它不起作用。
template<typename Vector>
void add(Vector& in, Vector& out, T& c) {
transform(in.begin(), in.end(), out.begin(), add_op<int>(c));
}
向量可能是两种类型:
device_vector<T>
host_vector<T>
add_op
结构看起来像这样:
template<typename T>
struct add_op : public thrust::unary_function<T,T> {
const T c;
add_op(T v) : c(v) {}
__host__ __device__
T operator()(const T x) {
return x + c;
}
};
简单地制作 T
add_vector_to_scalar
的另一个模板参数:
template<typename Vector, typename Scalar>
void add(const Vector& in, Vector& out, const Scalar& c) {
transform(in.begin(), in.end(), out.begin(), add_op<Scalar>(c));
}
请注意,我更改了in
和c
参数为const &
-由于它们仅输入参数,因此您不想(能够)在功能中修改它们。并将它们作为const &
允许传递临时性,而非const引用是不可能的。
相关文章:
- 部分专业化和嵌套模板
- C++-模板嵌套类的引用初始化无效
- 单独定义模板化嵌套类方法的正确语法
- 嵌套模板显式专用化
- 创建模板嵌套类实例的语法?
- 如何在C++中解析嵌套模板
- 具有嵌套类声明的类模板 (C++)
- 嵌套模板函数采用 2 个具有相同内部类型的容器
- 在嵌套类中使用模板值
- 具有模板 c++ 的嵌套类
- 嵌套模板函数的重载
- Boost.Python :C++模板类型匹配的嵌套命名空间
- 如何避免嵌套模板中的模板参数重复
- MSVC 2017 - 错误 - 如何将模板类 X 的模板成员函数声明为嵌套类 X::Y 的好友
- C++:不能使运算符<<成为模板嵌套类的好友
- 模板嵌套类模板类作为与完全专业函数的参数
- 如何为模板嵌套类编写可交换的重载算术运算符?
- 模板嵌套类的构造函数问题(复制 ctor 似乎覆盖了其他 ctor)
- 将模板嵌套类的方法定义移到声明之外
- c++中找不到模板嵌套类