C++ 2-phase lookup
This 2-phase look up of g++ (gnu C++) seems not inconsistency: builtin types are not treat equivalent with user defined type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> class A {}; void f(A) { printf("%s\n", __PRETTY_FUNCTION__); } class B {}; // now g(T) knew A,B,int,... // phase 1 lookup just success for f(A) template<class T> void g(T x) { f(x); } // after definition of g(T) and class B void f(B) { printf("%s\n", __PRETTY_FUNCTION__); } void f(int x) { printf("%s\n", __PRETTY_FUNCTION__); } void f(int*x) { printf("%s\n", __PRETTY_FUNCTION__); } int main(int argc, char** argv) { g(A()); // should always(phase 1 and phase 2) be OK g(B()); // f(B) definition is after g(T) definition, phase 1 just got f(A) g(B()); // but phase 2 got f(B) g(100); // intend to let g calling f(int), but failed in g++ 4.1 or newer g(&argc); // intention: similar as g(100) return 0; } |
I had checked the C++ standard text, gcc4.1 is conform with standard, and gcc3.4 is not standard conform.
Argument dependent name lookup requires argument to be classes/pointers to class objects.