+ 8
Why is the answer 11 and not 10?
11 Réponses
+ 8
That's kind of upsetting. C++ is a minefield dude.
I pulled up a recent-ish draft (N4713) of the standard, and there we have it, §6.4.1 "Unqualified Name Lookup", Clause 14:
If a variable member of a namespace is defined outside of the scope of its namespace then any name that
appears in the definition of the member (after the declarator-id) is looked up as if the definition of the
member occurred in its namespace.
[ Example:
namespace N {
int i = 4;
extern int j;
}
int i = 2;
int N::j = i; // N::j == 4
— end example ]
So the answer to your question is, "because the standard says so". Beats me.
Of course, if you explicitly call `::foo()` instead of `foo()`, you can call the function in the global scope.
+ 8
Schindlabua thanks. You never fail to impress!
+ 7
Schindlabua
yes C++ is minefield and very nice explanation😉. When I came across this topic in documentation "unqualified and qualified look-up "
Sonic in this when the definition of an variable is given outside of scope of the name space then the
Variable definition is searches in look-up table of unqualified name and then the variable declaration first search in the defined namespace
In this program struct foobar where you can assume foobar as an defined name space in this static variable int I and member function foo() is defined
for some read of the convention below are some links
https://en.cppreference.com/w/cpp/language/namespace
https://en.cppreference.com/w/cpp/language/unqualified_lookup
*/
/*https://stackoverflow.com/questions/31711183/how-does-unqualified-name-lookup-work-when-using-using-declarations
*/
Sonic
Look this code down for all the explanations.
https://code.sololearn.com/cvwo5aRv6hQx/?ref=app
+ 6
bahha that's not bad quiz though 😜 I just tangled with standards but if it confusing you can report via i mark at view correct answer.
+ 5
Why is foobar::foo() used here?
+ 5
bahha or rather, did the devil put it in the standard? Either way, I don't like to call community members or Stroustrup the devil 😊.
+ 5
GAWEN STEASY thanks.
+ 4
GAWEN STEASY any explanation if you are free?
+ 3
it's a weird design choice of C++ standard . the name lookup tries to not waste time looking for every identifier. so it stops at the closest scope. thus x get the return of the foo() inside of the struct.
the devil himself must have put that in the quiz factory.
+ 2
GAWEN STEASY 🤣, by the devil. I meant it's a tricky question, counter intuitive.
+ 1
Sonic My life for the community o7