This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Help needed with explicit instantiation of template methods
- From: David Aldrich <David dot Aldrich at EMEA dot NEC dot COM>
- To: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Wed, 6 Nov 2013 13:46:33 +0000
- Subject: Help needed with explicit instantiation of template methods
- Authentication-results: sourceware.org; auth=none
Hi
I would like some help to solve a C++ template specialisation problem please. I am working with gcc 4.6.3.
We have written a large class called 'Matrix' to manipulate matrices. The class is declared in Matrix.h and many of its methods are defined in Matrix.cpp. The class is used by other modules so, to satisfy the linker, I explicitly instantiate the necessary methods in Matrix.cpp. My problem concerns one of the those explicit instantiations.
Here is a small part of the declaration in Matrix.h:
template<class T> class Matrix : public MatrixBase
{
public:
<snip>
// Copy constructor
Matrix( const Matrix& a_X ) DONT_INLINE; // << === (A)
// Create a matrix from a matrix of another data type
template<class T1> Matrix<T>( const Matrix<T1>& a_X ) // << === (B)
{
<snip>
}
<snip>
}
Now, another module contains:
typedef Matrix< std::complex<double> > cmat;
unsigned eigHermitian( const cmat& a_X, cmat& a_V, rmat& a_D, unsigned a_numIter )
{
cmat A = a_X;
etc.
So I include the following explicit instantiation in Matrix.cpp to satisfy that copy constructor:
template Matrix<std::complex<double> >::Matrix<std::complex<double> >( const Matrix<std::complex<double> >& a_X ); // <== Line 2758
but when I compile Matrix.cpp I get a compiler error:
g++ -c -Wall -m32 -I/usr/include/python2.7 -I/usr/include/boost -O3 Matrix.cpp -o _gnuRelease/Matrix.o
Matrix.cpp:2758:10: error: ambiguous template specialisation 'Matrix<std::complex<double> >' for 'Matrix<std::complex<double> >::Matrix(const Matrix<std::complex<double> >&)'
Matrix.cpp:174:19: error: candidates are: Matrix<T>::Matrix(const Matrix<T>&) [with T = std::complex<double>]
Matrix.h:163:24: error: template<class T1> Matrix::Matrix(const Matrix<T1>&) [with T1 = T1, T = std::complex<double>]
/bin/sh: _gnuRelease/Matrix.d.4859: No such file or directory
So, it seems that the compiler does not know which to use of the two declarations (A) or (B) that I showed in my snippet of Matrix.h above.
How can I change the template specialisation to indicate to the compiler that it should use (A) ?
Best regards
David