Last active
August 29, 2015 14:13
-
-
Save dgodfrey206/1837948041c8ec4498e2 to your computer and use it in GitHub Desktop.
Example on ADL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#define LOG_ARG(ARG) \ | |
do { std::cout << __PRETTY_FUNCTION__ << std::endl; } while (0) | |
#define LOG LOG_ARG(__PRETTY_FUNCTION__) | |
class Widget { | |
class Toolbox { }; | |
public: | |
friend void foo(Widget*) { LOG; } | |
friend void bar(Toolbox) { LOG; } | |
Toolbox getBox() { return {}; } | |
}; | |
namespace N | |
{ | |
void baz(class X()) { LOG; } | |
class X { }; | |
} | |
int main() | |
{ | |
//foo(nullptr); // Error! nullptr has the associated namespace std and | |
// associated class std::nullptr_t, but ADL cannot | |
// find a function named foo. | |
foo(static_cast<Widget*>(nullptr)); // OK! If T is a pointer to U or an | |
// array of U, its associated | |
// namespaces and classes are those | |
// associated with U. | |
bar(Widget().getBox()); // OK! If T is a class type | |
// (including unions), its | |
// associated classes are: | |
// the class itself; the | |
// class of which it is a | |
// member, if any; [..] | |
extern N::X norf(); | |
baz(norf); // OK! If T is a function type, | |
// its associated namespaces | |
// and classes are those | |
// associated with the function | |
// parameter types and those | |
// associated with the return | |
// type. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment