473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C++ compiler error with templates

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;
}
Nov 17 '05 #1
3 2111
>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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #2
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
"David Lowndes" <da****@example.invalid> wrote in message
news:n0********************************@4ax.com...
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 reproducethe 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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq

Nov 17 '05 #3
>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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Matthew Del Buono | last post by:
Don't try to solve the problem. I've found a way -- around or fixing it. I'm just curious as to whether this is Microsoft's problem in their compiler or if there's a standard saying this is to be...
1
by: Generic Usenet Account | last post by:
Don't you often wish that compiler and linker messages for templates were a little less cryptic and misleading? Consider this: I was getting this error message: undefined reference to `vtable...
14
by: Mark Dufour | last post by:
After nine months of hard work, I am proud to introduce my baby to the world: an experimental Python-to-C++ compiler. It can convert many Python programs into optimized C++ code, without any user...
16
by: pj | last post by:
(Was originally, probably wrongly, posted to the vc subgroup.) (This doesn't appear to be a c# problem, but a problem with a bug in the Visual Studio c# compiler, but, any help will be welcome...)...
4
by: KK | last post by:
Hello all, I have class 'atr' which is based on templates. My idea was to initialize it two scenarios 1. std::string case 2. other data types I have defined the Init for the above different...
55
by: Steve | last post by:
I have to develop several large and complex C++ hardware test programs that should work under DOS, most likely with 32-bit DOS extender. Development workstation OS would be Microsoft XP. Quite some...
7
by: Chameleon | last post by:
This code below compiles fine with VS.2005 but with gcc 3.4.2 not. ------------------ template<class T> static void Wastage1D::clever_erase(vector<T> &v, vector<typename vector<T>::iterator>...
41
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
2
by: tropos | last post by:
I'm trying to use the 'typedef templates' idiom, mentioned in Alexandrescu's work on templates. Here's a Dr Dobbs article from 2002 that says they are a proposed C++ standard:...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.