Bug 31047 - GCC 4.1.1 has template bug
Summary: GCC 4.1.1 has template bug
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2007-03-05 17:11 UTC by satyakaam Goswami
Modified: 2007-03-06 17:10 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
test case (167 bytes, application/octet-stream)
2007-03-05 17:11 UTC, satyakaam Goswami
Details

Note You need to log in before you can comment on or make changes to this bug.
Description satyakaam Goswami 2007-03-05 17:11:27 UTC
We've uncovered a GCC 4.1.1 template bug when we compile using GCC4.1.1,This simple testcase shows the issue.

#include <iostream>

template< typename T > void foo( T var) { std::cout << "T" << std::endl; }

template < typename T> void doit( T var ) { foo(var); }

void foo(const int& var) { std::cout << "INT" << std::endl; }

int main() {
  doit(3);  // prints T
  return 0;
}

/////////////// temp.cpp ////////////////////////////////

Run the example with gcc 3.2.3

rm -f a.out
$/v3.2.3/bin/g++ -Wall temp.cpp
LD_LIBRARY_PATH=/v3.2.3/lib:$LD_LIBRARY_PATH; a.out

[~/test]$ gcc3
INT


Now run with GCC 4.1.1

[~/test]$ more gcc4
rm -f a.out
$/v4.1.1/bin/g++  -Wall temp.cpp
LD_LIBRARY_PATH=/v4.1.1/lib:$LD_LIBRARY_PATH; a.out

[georgef@sdvlx32 ~/test]$ gcc4
T

We can fix this particular example by moving 
void foo(const int& var) { std::cout << "INT" << std::endl; }
to the top of the function. However, this won't work in more complex cases.
Comment 1 satyakaam Goswami 2007-03-05 17:11:59 UTC
Created attachment 13146 [details]
test case
Comment 2 Andrew Pinski 2007-03-05 18:31:52 UTC
Actually this is not a bug.  The overloaded set for foo in doit is only the template foo and nothing else.  
Comment 3 Richard Biener 2007-03-06 10:42:49 UTC
No, this is a bug, 2nd stage name-lookup should find foo(const int&) as var is
dependent on the template parameter T (14.6/8, /9).
But I bet we have a dup for this.
Comment 4 Andrew Pinski 2007-03-06 17:10:29 UTC
(In reply to comment #3)
> No, this is a bug, 2nd stage name-lookup should find foo(const int&) as var is
> dependent on the template parameter T (14.6/8, /9).
> But I bet we have a dup for this.

14.6.4.2/1 says this is invalid code.
For a function call that depends on a template parameter, if the function is an unqualified-id but not a template-id, the candiate functions are found using the usual lookup rules (3.4.1, 3.4.2) except that:
....

Paraphasing since I don't want to type it all in
* non argument dependent lookup (3.4.1: template definition context
* argument dependent lookup (3.4.2):  template definition context and template instation context

Since the type is a fundamental type, argument dependent lookup does not apply, therefor this is not a bug.