21st Nov 2019, 10:33 PM
Sonic
Sonic - avatar
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.
22nd Nov 2019, 1:31 AM
Schindlabua
Schindlabua - avatar
+ 8
Schindlabua thanks. You never fail to impress!
22nd Nov 2019, 2:45 AM
Sonic
Sonic - avatar
+ 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
22nd Nov 2019, 3:06 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 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.
22nd Nov 2019, 8:18 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 5
Why is foobar::foo() used here?
21st Nov 2019, 11:52 PM
Sonic
Sonic - avatar
+ 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 😊.
22nd Nov 2019, 2:44 AM
Sonic
Sonic - avatar
+ 5
GAWEN STEASY thanks.
22nd Nov 2019, 4:29 AM
Sonic
Sonic - avatar
+ 4
GAWEN STEASY any explanation if you are free?
21st Nov 2019, 10:33 PM
Sonic
Sonic - avatar
+ 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.
22nd Nov 2019, 2:18 AM
Bahhaⵣ
Bahhaⵣ - avatar
+ 2
GAWEN STEASY 🤣, by the devil. I meant it's a tricky question, counter intuitive.
22nd Nov 2019, 8:31 AM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
Sonic My life for the community o7
22nd Nov 2019, 11:38 AM
Schindlabua
Schindlabua - avatar