Pattern matching in g ++

I have a weird problem, and I wonder why g ++ 4.1.2 behaves the way it does.

The division is essentially:

#include <iostream> template<typename T> inline void f(T x) { std::cout << x*x; } namespace foo { class A { public: void f() const { f(2); } }; } 

Calling f(2) not possible because the compiler does not match the template function f . I can make it work with ::f(2) , but I would like to know WHY it is necessary, since it is absolutely unambiguous, and as far as I know (admittedly outdated) knowledge about the rules of compliance, this should work.

+4
source share
3 answers

The compiler checks all scopes for the candidate, starting with the current scope. It finds a function named f in the immediate area, and stops the search . Your version of the template is never considered a candidate.

See Namespaces and Interface Principles for more information .

+12
source

Refer to C ++ 03 section

3.4.1 Search for an unqualified name

In all cases listed in 3.4.1, the search area searches for a declaration in the order specified in each of the relevant categories; the name search ends as soon as an announcement for the name is announced . If the ad is not found, the program is poorly formed.

In your code example, the compiler finds the name f in the current area, thereby ending the search for an unqualified name, but there is a mismatch in the function prototypes, so you get an error message.

Qualifying this parameter with :: makes it work, because the name is then executed in the global namespace and f is called with the correct prototype.

+5
source

It seems that the compiler is trying to call A :: f and is failing due to an argument that seems normal in some way. Do you have the same error if you use a function without a template?

+2
source

All Articles