I did that (or thought I did) but the code I produced compiles with gcc 3.4.2 but not with Microsoft Visual Studio 2008 (Express Edition). So I have reduced my code to a minimum compilable example that exhibits the behaviour as follows
Expand|Select|Wrap|Line Numbers
- template<class T>
- class Example
- {
- template<class TT> template<class RHS> friend void Example<TT>::copy(RHS& rhs);
- public:
- // Default constructor
- Example() : data(0) { }
- //Destructor deletes managed object when reference count is zero
- ~Example(){ delete data; }
- template<class R>
- R* asType() { return data; }
- template<class R>
- const R* asType() const { return data; }
- template<class RHS>
- void copy(RHS& rhs);
- protected :
- T* data;
- };
- template<class T>
- template<class RHS>
- void Example<T>::copy(RHS& rhs)
- {
- this->~Example();
- data = rhs.template asType<T>();
- }
- int main(int, char**)
- {
- Example<int> ei;
- Example<int> el;
- ei.copy<Example<int> >(el);
- }
So for gcc 3.4.2 using the command line
g++ -Wall -pendantic TemplateText.cpp
compiles into an executable without producing any compiler diagnostic messages.
However for Microsoft cl version 15.00.30729.01 (as shipped with Visual Studio 2008 Express Edition) the following errors are produced
Expand|Select|Wrap|Line Numbers
- TemplateTest.cpp(4) : warning C4346: 'Example<T>::copy' : dependent name is not a type
- prefix with 'typename' to indicate a type
- TemplateTest.cpp(25) : see reference to class template instantiation 'Example<T>' being compiled
- TemplateTest.cpp(4) : error C2998: 'Unique-Type-copy copy' : cannot be a template definition
- TemplateTest.cpp(8) : error C3861: 'data': identifier not found
- TemplateTest.cpp(8) : error C2461: 'Example<T>' : constructor syntax missing formal parameters
- TemplateTest.cpp(8) : error C2473: '{ctor}' : looks like a function definition, but there is no parameter list.
- TemplateTest.cpp(11) : error C2143: syntax error : missing ';' before '~'
- TemplateTest.cpp(34) : error C2039: 'copy' : is not a member of 'Example<T>'
- TemplateTest.cpp(4) : warning C4346: 'Example<T>::copy' : dependent name is not a type
- prefix with 'typename' to indicate a type
- TemplateTest.cpp(39) : see reference to class template instantiation 'Example<T>' being compiled
- with
- [
- T=int
- ]
- TemplateTest.cpp(4) : error C2998: 'Unique-Type-copy copy' : cannot be a template definition
- TemplateTest.cpp(8) : error C3861: 'data': identifier not found
- TemplateTest.cpp(8) : error C2461: 'Example<T>' : constructor syntax missing formal parameters
- with
- [
- T=int
- ]
- TemplateTest.cpp(8) : error C2473: '{ctor}' : looks like a function definition, but there is no parameter list.
- TemplateTest.cpp(11) : error C2143: syntax error : missing ';' before '~'
- TemplateTest.cpp(43) : error C2039: 'copy' : is not a member of 'Example<T>'
- with
- [
- T=int
- ]
Alternatively can anyone think of a way of solving the problem described in the first paragraph?