Last active
April 9, 2023 03:10
-
-
Save uucidl/bf877d760fa1b75265b3edf078be980f to your computer and use it in GitHub Desktop.
ADL Barrier Idiom
This file contains 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
// What you do when you want to prevent ADL from kicking in, and force your | |
// users to use fully qualified names. | |
namespace module | |
{ | |
struct Foo { int a, b; }; // struct defined in one namespace | |
namespace functions | |
{ | |
void Flaggerbify(Foo* foo); // function defined in another | |
} | |
using namespace module::functions; // we pretend functions are in same namespace (the public one) | |
} // namespace module | |
// Usage code: | |
int main() | |
{ | |
module::Foo a_foo; | |
#if ADL_NOT_PREVENTED | |
// This call would work if Faggerbify was really in the same namespace as module::Foo, due to ADL | |
// @url: http://en.cppreference.com/w/cpp/language/adl | |
Flaggerbify(&a_foo); | |
#endif | |
// Instead, the user has to do this (or import the whole module) | |
module::Flaggerbify(&a_foo); | |
}; | |
// Implementation, not interesting: | |
namespace module | |
{ | |
namespace functions | |
{ | |
void Flaggerbify(Foo* foo) | |
{ | |
foo->a *= -1; | |
foo->b *= -1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment