r/cpp • u/Naive_Faithlessness1 • Mar 22 '26
namespace dependent lookup ?
Hi !
I'm kind of deceived that it does not work like this. Here's some code:
int
Here's the compiler output:
<source>:9:28: error: 'apple' was not declared in this scope; did you mean 'me::fruit::apple'?
9 | auto l = me::fruit::lychee(apple{}, pear{});
| ^~~~~
| me::fruit::apple
<source>:2:12: note: 'me::fruit::apple' declared here
2 | struct apple { /*...*/ };
| ^~~~~
<source>:9:37: error: 'pear' was not declared in this scope; did you mean 'me::fruit::pear'?
9 | auto l = me::fruit::lychee(apple{}, pear{});
| ^~~~
| me::fruit::pear
<source>:3:12: note: 'me::fruit::pear' declared here
3 | struct pear { /*...*/ };
As you can see the compiler is perfectly able to deduce what I mean by apple and pear but still, it doesn't compile !
I'm surprised that there still no rule in the C++ Standard to handle this kind of situation ; it seems so obvious to me that it should work this way.
I know that I could write :
int
Believe it or not, I find this code less than clear. It seems clear here because it's an example and like all examples, it's simple, but in a real-world situation, this code will undoubtedly be surrounded by other lines of code, meaning that this using directive could lead to the problems we all know.
To me this syntax remains much clearer :
int
Are we therefore condemned to have to write :
auto l = ;
There may be solutions I'm not aware of; if so, please let me know!
Thanks !
View all comments
Show parent comment
-6
u/Naive_Faithlessness1 Mar 22 '26 edited Mar 22 '26
I reply to you but also to others that according to me the compiler have enough information (here, the
lycheeconstructor) to safely deduce the right type :you::appleis different thanme::appleand since thelycheeconstructor is only able to receive thatme::appleI don't see where it can get wrong...There may be technical reasons why it might not work that I'm not aware of, but I don't think it's the one you mentioned.