I am trying to enhance the readability of my library which features a matrix class. In particular I want users to get a direct overview when looking at the class definition. Therefore I am moving the actual functions outside of the definition. However, I appear to be unsuccessful for the copy constructor. In particular I don't succeed to define the templates correctly.
A minimal example (featuring the functions outside the class definition) is the following:
#include <vector>
template<class X>
class matrix
{
private:
std::vector<X> m_data;
public:
matrix(){};
matrix(size_t n);
X& operator[](size_t i);
size_t size();
template<\
typename U,typename V=X,\
typename=typename std::enable_if<std::is_convertible<X,U>::value>::type\
>
operator matrix<U> ()
{
matrix<U> out(size());
for ( size_t i = 0 ; i < size() ; ++i )
out[i] = static_cast<U>( m_data[i] );
return out;
}
};
template<class X>
matrix<X>::matrix(size_t n)
{
m_data.resize(n);
}
template<class X>
X& matrix<X>::operator[](size_t i)
{
return m_data[i];
}
template<class X>
size_t matrix<X>::size()
{
return m_data.size();
}
int main()
{
matrix<double> A(3);
matrix<int> B = A;
}
Assuming your question refers to the conversion operator template ( you only have the implicit copy costructor there ), the syntax to move out a member template requires the specification of both template-parameters ( class first, without defaults ), hence:
template<class X>
template<typename U,typename V,typename E>
matrix<X>::operator matrix<U> ()
{
//...
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments