0

I wonder why the following snipped of C++ code compiles without error:

namespace ns {
    struct foo {};
    void stuff(void*x) {}
}
int main() {
    ns::foo x;
    stuff(&x);
}

I thought that it must be obligatory to put a namespace qualifier in front of stuff, and indeed if I replace the variable x with int x, then the error arises. It appears that the compiler deduces the namespace to which stuff belongs from the type of its argument, which doesn't make sense to me. Tested with GCC 4.2.1 and 4.8.3

user3097263
  • 63
  • 1
  • 5
  • 5
    Due to argument dependent lookup (ADL). See: http://stackoverflow.com/questions/8111677/what-is-argument-dependent-lookup-aka-adl-or-koenig-lookup –  Aug 07 '15 at 14:08
  • See http://stackoverflow.com/questions/8111677/what-is-argument-dependent-lookup-aka-adl-or-koenig-lookup – juanchopanza Aug 07 '15 at 14:10
  • ADL indeed. Declare the x inside main as int and you get a compilation error. – Anonymous Coward Aug 07 '15 at 14:14

0 Answers0