c++ - 为模板特化提供*隐式*转换运算符

标签 c++ templates type-conversion template-specialization

我有一个模板 sparse_vector<T>类,我也在使用 Boost UBLAS。我将如何在 sparse_vector<double> 之间提供隐式 转换?和 boost::numeric::ublas::compressed_vector<double>

我还想在 std::vector<double> 之间提供类似的转换和 boost::numeric::ublas::vector<double> .

(我正在使用启用了 C++0x 的 gcc 4.4。)

最佳答案

我假设这些类型有一个迭代器构造函数。如果这个假设是准确的,那么它就像写这样的东西一样简单:


template < typename OutputVector, typename InputVector >
OutputVector vector_cast(InputVector const& input)
{
  return OutputVector(input.begin(), input.end());
}

像这样使用:compressed_vector<double> vect = vector_cast<compressed_vector<double> >(my_sparse_vector);

关于c++ - 为模板特化提供*隐式*转换运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2993020/

相关文章:

c++ - 模板类型列表中的递归取消引用,如何返回正确的类型

r - 数值转换 R 中的 as.factor()

c# - 为什么不允许从 "class A : IX"到通用 "T where T : IX"的转换?

c++ - 在不存在的对象上调用静态成员函数

c++ - 错误 C2065,我不知道出了什么问题

c++ - 需要大尺寸静态数组

c++ - 防止不必要的 C++ 仿函数对象拷贝

c++ - 模板类相互使用会产生歧义错误

c++ - C++中字符串文字的数据类型是什么?

c++ - MFC 无法生成动态 DLL 文件