C++ compiler error with templates

  • Thread starter Thread starter Benedikt Weber
  • Start date Start date
B

Benedikt Weber

I found the following compiler error with Microsoft Visual C++ .NET.

I use different functions with return types determined by a Traits class.
Function g() below works ok, but when I put the two declarations f1() and
f2(), the compiler gets disturbed. The error message does not even reproduce
the original code correctly. When the two declarations are switched, the
problem goes away.

Benedikt

//----------------------------------------

#include "stdafx.h"
template<class S,class T> struct Traits;

template<>
struct Traits<double,double>
{
typedef double RType;
};

template<class T> class Matrix
{
public: typedef T Type;
};

template<class U, class V>
Matrix<typename Traits<typename U::Type,typename V::Type>::RType>
f1(const U& mat1, const V& mat2);

template<class U, class V>
Matrix<typename Traits<typename U::Type,V>::RType>
f2(const U& mat1, const Matrix<V>& mat2);

template<class U, class V>
Matrix<typename Traits<typename U::Type,typename V::Type>::RType>
g(const U& mat1, const V& mat2)
{
return Matrix<U::Type>();
}

int _tmain(int argc, _TCHAR* argv[])
{
Matrix<double> A;
Matrix<double> B;
g(A,B);
/*
error C2893: Failed to specialize function template
'Matrix<Traits<U::Type,V>::RType> g(const U &,const V &)'

Notice how the template argument has changed from
Traits<typename U::Type,typename V::Type>
to
Traits<U::Type,V>
There is no problem if the declarations of f1 and f2 change order!!
*/
return 0;
}
 
I found the following compiler error with Microsoft Visual C++ .NET.
I use different functions with return types determined by a Traits class.
Function g() below works ok, but when I put the two declarations f1() and
f2(), the compiler gets disturbed. The error message does not even reproduce
the original code correctly. When the two declarations are switched, the
problem goes away.

Benedikt,

I don't have an explanation for the curious behaviour, but trying your
code with the online Comeau compiler indicates that even without the 2
functions that confuse VC++, your code is perhaps not correct:

"ComeauTest.c", line 26: error: nontype "U::Type" is not a type name
return Matrix<U::Type>();

changing the line:
return Matrix<U::Type>();
to
return Matrix< typename U::Type>();

allows a clean compilation with Comeau. However it doesn't remedy the
problem that the MS compiler shows with the 2 functions where they are
in the source code. It's really strange that simply re-ordering the
functions (put g before f1 & f2 and it's alright) causes a problem and
the problem also shows in the alpha Whidbey compiler so I'll try to
report it to MS.

If this is a issue that you can't work-around, and no-one else has any
suggestions, I suggest that you telephone MS Product Support.

Dave
 
David

Yes, I forgot the typename when making up the small example and VC++ did not
complain. My workaround in my project is an ordering that works, although it
reduces the readability.

I am quite sure it's a compiler bug but I don't know how to report this to
MS (I have a campus license). That's why I posted it here. If somebodey can
report it to MS, that would be nice.

Benedikt
 
I am quite sure it's a compiler bug but I don't know how to report this to
MS (I have a campus license). That's why I posted it here. If somebodey can
report it to MS, that would be nice.

I've submitted a bug report for it.

Dave
 
Back
Top