Skip to content

Instantly share code, notes, and snippets.

@uucidl
Last active April 9, 2023 03:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uucidl/bf877d760fa1b75265b3edf078be980f to your computer and use it in GitHub Desktop.
Save uucidl/bf877d760fa1b75265b3edf078be980f to your computer and use it in GitHub Desktop.
ADL Barrier Idiom
// 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