VC++ 2003 compiler bug 
Author Message
 VC++ 2003 compiler bug

 I have found a bug in the compiler with friend template
functions as below - the compiler seems to forget what
the return type is when a template friend function is
used as a declaration (You can actually get a variety of
different errors and workarounds depending on whether the
classes and functions in the sample below are templated
or not)
  Andrew
----------------------------------------------------------
---------------

template <class T>
class bar
{
  template <class T> friend int foo();

Quote:
};

// Adding a definition fixes the bug below
//template <class T> int foo(){return 6;}

template <class T>
int bip()
{ return foo<int>(); } // error C2440: 'return' : cannot
convert from 'void' to 'int'

int _tmain(int argc, _TCHAR* argv[])
{
  bar<int> b;
  bip<int>();
  return 0;

Quote:
}



Mon, 24 Oct 2005 21:38:14 GMT  
 VC++ 2003 compiler bug
Andrew,

Quote:
> template <class T>
> class bar
> {
>   template <class T> friend int foo();

IMHO this should be a compiler error because T is already
defined in class scope.

Quote:
> };

> // Adding a definition fixes the bug below
> //template <class T> int foo(){return 6;}

> template <class T>
> int bip()
> { return foo<int>(); } // error C2440: 'return' : cannot
> convert from 'void' to 'int'

Even though I absolutely admit that this is a very poor
message, I think the compiler is right to issue an error here.
'foo' should not be found by ordinary name lookup.

Quote:
> //template <class T> int foo(){return 6;}

Note that the definition is not for the foo in the lexical
scope of bar. It is for a function template foo in the
root namespace which can be (and actually is ) found by
name lookup.

-hg



Mon, 24 Oct 2005 23:49:45 GMT  
 VC++ 2003 compiler bug
This code is not legal C++, as Holger has already pointed out.  The error
that VC7.1 is providing is abysmal, it's true.

Comeau reports the following:

Comeau C/C++ 4.3.1 (Mar  1 2003 20:09:34) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing.  All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 4: error: template parameter "T" may not be redeclared
in this
          scope
    template <class T> friend int foo();
                    ^

"ComeauTest.c", line 12: error: identifier "foo" is undefined
  { return foo<int>(); }
           ^

"ComeauTest.c", line 12: error: type name is not allowed
  { return foo<int>(); }
               ^

"ComeauTest.c", line 12: error: expected an expression
  { return foo<int>(); }
                    ^

"ComeauTest.c", line 4: error: template parameter "T" may not be redeclared
in this
          scope
    template <class T> friend int foo();
                    ^
          detected during instantiation of class "bar<T> [with T=int]" at
line
                    17

There are basically two errors:
    1. The re-use of the template formal parameter name T in the declaration
of the friend template foo<>.
    2. The assumption that the friend declaration is sufficient to make
foo<> visible at file scope.  It is not.  It is only sufficient to make
foo<> visible from within member functions of class bar<T> according the to
C++ standard.

-cd

Quote:

> I have found a bug in the compiler with friend template
> functions as below - the compiler seems to forget what
> the return type is when a template friend function is
> used as a declaration (You can actually get a variety of
> different errors and workarounds depending on whether the
> classes and functions in the sample below are templated
> or not)
>   Andrew
> ----------------------------------------------------------
> ---------------

> template <class T>
> class bar
> {
>   template <class T> friend int foo();
> };

> // Adding a definition fixes the bug below
> //template <class T> int foo(){return 6;}

> template <class T>
> int bip()
> { return foo<int>(); } // error C2440: 'return' : cannot
> convert from 'void' to 'int'

> int _tmain(int argc, _TCHAR* argv[])
> {
>   bar<int> b;
>   bip<int>();
>   return 0;
> }



Tue, 25 Oct 2005 00:19:07 GMT  
 VC++ 2003 compiler bug
I didnt bother looking up 1 in the spec since its less
confusing to use a different template parameter anyway

You are right about #2 - I believe you are referring to
7.3.1.2 - I guess I learned something new :)

 BUT it still seems to be a bug - here is a modified
(hopefully now correct) version that exhibits the same
behavior

#include "stdafx.h"
template <class T>
class bar
{
  template <class S> friend int foo();

Quote:
};

template <class S> int foo();

template <class T>
int bip()
{ return foo<int>(); } // error C2440: 'return' : cannot
convert from 'void' to 'int'

int _tmain(int argc, _TCHAR* argv[])
{
  bar<int> b;
  bip<int>();
  return 0;

Quote:
}
>-----Original Message-----
>This code is not legal C++, as Holger has already

pointed out.  The error
Quote:
>that VC7.1 is providing is abysmal, it's true.

>Comeau reports the following:

>Comeau C/C++ 4.3.1 (Mar  1 2003 20:09:34) for

ONLINE_EVALUATION_BETA1
Quote:
>Copyright 1988-2003 Comeau Computing.  All rights
reserved.
>MODE:strict errors C++

>"ComeauTest.c", line 4: error: template parameter "T"

may not be redeclared
Quote:
>in this
>          scope
>    template <class T> friend int foo();
>                    ^

>"ComeauTest.c", line 12: error: identifier "foo" is
undefined
>  { return foo<int>(); }
>           ^

>"ComeauTest.c", line 12: error: type name is not allowed
>  { return foo<int>(); }
>               ^

>"ComeauTest.c", line 12: error: expected an expression
>  { return foo<int>(); }
>                    ^

>"ComeauTest.c", line 4: error: template parameter "T"

may not be redeclared
Quote:
>in this
>          scope
>    template <class T> friend int foo();
>                    ^
>          detected during instantiation of class "bar<T>
[with T=int]" at
>line
>                    17

>There are basically two errors:
>    1. The re-use of the template formal parameter name

T in the declaration
Quote:
>of the friend template foo<>.
>    2. The assumption that the friend declaration is
sufficient to make
>foo<> visible at file scope.  It is not.  It is only
sufficient to make
>foo<> visible from within member functions of class

bar<T> according the to

- Show quoted text -

Quote:
>C++ standard.

>-cd


>> I have found a bug in the compiler with friend template
>> functions as below - the compiler seems to forget what
>> the return type is when a template friend function is
>> used as a declaration (You can actually get a variety
of
>> different errors and workarounds depending on whether
the
>> classes and functions in the sample below are templated
>> or not)
>>   Andrew
>> -------------------------------------------------------
---
>> ---------------

>> template <class T>
>> class bar
>> {
>>   template <class T> friend int foo();
>> };

>> // Adding a definition fixes the bug below
>> //template <class T> int foo(){return 6;}

>> template <class T>
>> int bip()
>> { return foo<int>(); } // error C2440: 'return' :
cannot
>> convert from 'void' to 'int'

>> int _tmain(int argc, _TCHAR* argv[])
>> {
>>   bar<int> b;
>>   bip<int>();
>>   return 0;
>> }

>.



Tue, 25 Oct 2005 01:55:04 GMT  
 VC++ 2003 compiler bug
That would be a bug alright!
-cd
Quote:

> I didnt bother looking up 1 in the spec since its less
> confusing to use a different template parameter anyway

> You are right about #2 - I believe you are referring to
> 7.3.1.2 - I guess I learned something new :)

>  BUT it still seems to be a bug - here is a modified
> (hopefully now correct) version that exhibits the same
> behavior

> #include "stdafx.h"
> template <class T>
> class bar
> {
>   template <class S> friend int foo();
> };

> template <class S> int foo();

> template <class T>
> int bip()
> { return foo<int>(); } // error C2440: 'return' : cannot
> convert from 'void' to 'int'

> int _tmain(int argc, _TCHAR* argv[])
> {
>   bar<int> b;
>   bip<int>();
>   return 0;
> }
>> -----Original Message-----
>> This code is not legal C++, as Holger has already
> pointed out.  The error
>> that VC7.1 is providing is abysmal, it's true.

>> Comeau reports the following:

>> Comeau C/C++ 4.3.1 (Mar  1 2003 20:09:34) for ONLINE_EVALUATION_BETA1
>> Copyright 1988-2003 Comeau Computing.  All rights reserved.
>> MODE:strict errors C++

>> "ComeauTest.c", line 4: error: template parameter "T"
> may not be redeclared
>> in this
>>          scope
>>    template <class T> friend int foo();
>>                    ^

>> "ComeauTest.c", line 12: error: identifier "foo" is undefined
>>  { return foo<int>(); }
>>           ^

>> "ComeauTest.c", line 12: error: type name is not allowed
>>  { return foo<int>(); }
>>               ^

>> "ComeauTest.c", line 12: error: expected an expression
>>  { return foo<int>(); }
>>                    ^

>> "ComeauTest.c", line 4: error: template parameter "T"
> may not be redeclared
>> in this
>>          scope
>>    template <class T> friend int foo();
>>                    ^
>>          detected during instantiation of class "bar<T> [with
>> T=int]" at line
>>                    17

>> There are basically two errors:
>>    1. The re-use of the template formal parameter name
> T in the declaration
>> of the friend template foo<>.
>>    2. The assumption that the friend declaration is sufficient to
>> make foo<> visible at file scope.  It is not.  It is only sufficient
>> to make foo<> visible from within member functions of class bar<T>
>> according the to C++ standard.

>> -cd


>>> I have found a bug in the compiler with friend template
>>> functions as below - the compiler seems to forget what
>>> the return type is when a template friend function is
>>> used as a declaration (You can actually get a variety of
>>> different errors and workarounds depending on whether the
>>> classes and functions in the sample below are templated
>>> or not)
>>>   Andrew
>>> ------------------------------------------------------- ---
>>> ---------------

>>> template <class T>
>>> class bar
>>> {
>>>   template <class T> friend int foo();
>>> };

>>> // Adding a definition fixes the bug below
>>> //template <class T> int foo(){return 6;}

>>> template <class T>
>>> int bip()
>>> { return foo<int>(); } // error C2440: 'return' : cannot
>>> convert from 'void' to 'int'

>>> int _tmain(int argc, _TCHAR* argv[])
>>> {
>>>   bar<int> b;
>>>   bip<int>();
>>>   return 0;
>>> }

>> .



Tue, 25 Oct 2005 09:41:42 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. VC++ 2003 c2.dll bug D2029

2. VC .NET 2003 l-value bug?

3. 2 bugs in VC++ 2003

4. VC++ 2003 template bug?

5. VC.NET 2003 IDE bug report

6. Very probably a bug in the code generator of VC++ 2003

7. VC++ 2003: Internal compiler error on wchar_t const* in constructor

8. Final Release VC++ 2003 - Internal Compiler Errors

9. 64 bit compiler warnings - upgrade to 2003 VC++

10. Problem porting ATL project from VC++ 6.0 to VC++.NET 2003

11. Converting project from VC++ 2002 to VC++ 2003 and adding a new configuration

12. Slower compilation with VC Net 2003 (7.1) when compared to VC Net 7.0

 

 
Powered by phpBB® Forum Software