This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Help needed with explicit instantiation of template methods
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: David Aldrich <David dot Aldrich at emea dot nec dot com>
- Cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Wed, 6 Nov 2013 14:22:30 +0000
- Subject: Re: Help needed with explicit instantiation of template methods
- Authentication-results: sourceware.org; auth=none
- References: <41302A7145AC054FA7A96CFD03835A0A0B7A378B at EX10MBX02 dot EU dot NEC dot COM>
On 6 November 2013 13:46, David Aldrich wrote:
> Hi
>
> I would like some help to solve a C++ template specialisation problem please. I am working with gcc 4.6.3.
This isn't really a GCC question, it's just about general C++.
> 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.
Can't you simply define them in the header?
> 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:
It would be a lot more helpful if you show a complete example, reduced
to just the pertinent details, not small parts. Here's a complete
example:
template<typename T>
class Matrix
{
public:
Matrix(){}
Matrix(const Matrix&);
template<typename U>
Matrix(const Matrix<U>&) { }
};
template<typename T>
Matrix<T>::Matrix(const Matrix&) {}
template Matrix<int>::Matrix(const Matrix&);
int main()
{
Matrix<int> m;
Matrix<int> mm = m;
}
> How can I change the template specialisation to indicate to the compiler that it should use (A) ?
I'm not sure what the right syntax is, different compilers behave differently.
Can you simply instantiate the whole class, instead of the copy constructor?
template class Matrix< std::complex<double> >;