[Bug c++/53573] template type dependent name resolution broken
keean@fry-it.com
gcc-bugzilla@gcc.gnu.org
Mon Jun 4 17:13:00 GMT 2012
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53573
--- Comment #5 from Keean Schupke <keean@fry-it.com> 2012-06-04 17:13:18 UTC ---
The following program will apparently compile correctly, but will silently
produce incorrect results at runtime:
#include <iostream>
char g(char x) {
return x - 1;
}
template <typename T> T f(T t) {
return g(t);
}
int g(int x) {
return x + 1;
}
main() {
int x(1);
int y(f(x));
std::cout << y << "\n";
int z(f<int>(x));
std::cout << z << "\n";
}
This should print "2 2" but prints "0 0". Interestingly it also produces
incorrect results with gcc-4.6.3 and gcc-4.5.3. However the code below (which
does not compile on gcc-4.7.0 does produce the correct results on gcc-4.6.3 and
gcc-4.5.3:
#include <iostream>
template <typename T> T f(T t) {
return g(t);
}
char g(char x) {
return x - 1;
}
int g(int x) {
return x + 1;
}
main() {
int x(1);
int y(f(x));
std::cout << y << "\n";
int z(f<int>(x));
std::cout << z << "\n";
}
So it looks like earlier versions of GCC relied on 'g' being undefined at the
point of definition to defer binding until instantiation instead of looking for
dependence on a template parameter. gcc-4.7.0 does not seem to do either.
One more example:
#include <iostream>
class A {};
A g(A x) {
return x;
}
template <typename T> T f(T t) {
return g(t);
}
int g(int x) {
return x + 1;
}
main() {
int x(1);
int y(f(x));
std::cout << y << "\n";
int z(f<int>(x));
std::cout << z << "\n";
}
generates the following compile error:
test.cpp: In function ‘T f(T) [with T = int]’:
test.cpp:35:11: instantiated from here
test.cpp:26:12: error: could not convert ‘t’ from ‘int’ to ‘A’
More information about the Gcc-bugs
mailing list