0

I'm testing the following code:

#include <iostream> template<typename T> class A { public: void koo(T) { std::cout << "Hello world!"; } }; template <typename T> class B : public A<T> { public: void pun(T i) { koo(i); } }; int main() { B<int> boo; boo.pun(5); } 

with compilation info as:

main.cpp:12:24: error: ‘koo’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] 12 | void pun(T i) { koo(i); } | ~~~^~~ main.cpp:12:24: note: declarations in dependent base ‘A’ are not found by unqualified lookup main.cpp:12:24: note: use ‘this->koo’ instead 

I know I can avoid this error with this->koo(i) or A<T>::koo(i), but I want to understand why this compilation error happens.

I think koo in pun definition is a dependent name, according to dependent name / lookup rules "the lookup of a dependent name used in a template is postponed until the template arguments are known". In the main function, B<int> boo; sets the template parameter as int. Then why ADL doesn't work for the function expression koo(i) ? ————————————————————————————————

And let's put ADL aside momentarily. If I change void pun(T i) { koo(i); } to void pun(T i) { goo(i); }, now the new compilation info is:

main.cpp:12:24: error: ‘goo’ was not declared in this scope; did you mean ‘koo’? 12 | void pun(T i) { goo(i); } | ~~~^~~ | koo 

Why compilation info for the two cases are different? The new error doesn't mention "argument-dependent lookup" at all.

2
  • 1
    ADL only finds free functions. And that has nothing to do with your issue here. Commented Oct 16, 2021 at 10:31
  • You can also explicitly bring koo into B’s space with using A<T>::koo; anywhere you find convenient. Commented Oct 16, 2021 at 11:25

1 Answer 1

0

ADL is argument dependent lookup. A function name is looked for in the associated namespaces of its arguments. int has no associated namespaces, so no ADL occurs. Even if it did occur, it would only find free functions, so your method would not be found.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm still kinda confused. Could you send a link to references stating that ADL only find free functions, not for member functions? Besides, could you look into the new compilation error when void pun(T i) { koo(i); } is changed to void pun(T i) { goo(i); }?
@neymar Look at ADL on cppreference or read the standard. It is difficult to read, but what do you want, some blog post by some random person? ADL doesn't find methods. The standard does not mandate the form of error messages; VS is gicing you different hints to what you did wrong, but the same problem underlies both. "I don't believe you" is not confused. I am telling you why ADL doesn't do what you think it does. You just don't believe it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.