为模板特化提供* implicit *转换运算符

时间:2010-06-07 20:45:25

标签: 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>之间提供类似的转换。

(我使用的是gcc 4.4,启用了C ++ 0x。)

1 个答案:

答案 0 :(得分:1)

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


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);