C++ 为模板专门化提供*隐式*转换运算符
我有一个模板化的C++ 为模板专门化提供*隐式*转换运算符,c++,templates,type-conversion,template-specialization,C++,Templates,Type Conversion,Template Specialization,我有一个模板化的sparse_vector类,我也在使用boostublas。如何在sparse_vector和boost::numeric::ublas::compressed_vector之间提供隐式转换 我还想在std::vector和boost::numeric::ublas::vector之间提供类似的转换 (我使用的是启用了C++0x的GCC4.4。)我假设这些类型有一个迭代器构造函数。如果这个假设是正确的,那么就简单得像这样写: template < typename Ou
sparse_vector
类,我也在使用boostublas。如何在sparse_vector
和boost::numeric::ublas::compressed_vector
之间提供隐式转换
我还想在std::vector
和boost::numeric::ublas::vector
之间提供类似的转换
(我使用的是启用了C++0x的GCC4.4。)我假设这些类型有一个迭代器构造函数。如果这个假设是正确的,那么就简单得像这样写:
template < typename OutputVector, typename InputVector >
OutputVector vector_cast(InputVector const& input)
{
return OutputVector(input.begin(), input.end());
}
模板
输出向量向量\u转换(输入向量常量和输入)
{
返回OutputVector(input.begin(),input.end());
}
像这样使用:
compressed\u vector vect=vector\u cast(my\u sparse\u vector)代码>感谢您抽出时间回答。我实际上是在问是否可以编写隐式转换,这样我就可以在需要boost::…::vector的上下文中使用std::vector。(我对问题进行了编辑以使其更清楚。)这是不可能的。您可以覆盖每个这样的函数,然后使用上面的显式“cast”调用原始函数,但这是您在不实际更改相关对象的情况下所能得到的最接近的结果。谢谢。我只是想确定这是不可能的。是否可能在模板类中有一个成员函数,根据模板类型有条件地添加?是的。看看boost::enable_if