Yes and no. Mostly no.
The bad news is if an enum is outside the current scope, such as Tuesday
, etc then it can't be passed to a function, even if that function was declared in a namespace where the enum was visible. This is because argument lookup occurs first when you write a function call and the arguments can not be passed to gun
and then have name lookup occur. Nothing can change this - however there is good news too.
Firstly you seem to need behaviour that maps ns::foo(arg1, arg2)
-> {using namespace ns; ns::foo(arg1, arg2);}
. Function calls and templates can't change this but macros kind of can and I included and example.
Also I gave a basic example of argument dependent lookup. You can see that the out-of-scope functions GetMonday and GetTuesday (which return your out-of-scope enum) can be found using this mechanism simply because you included one type from that namespace. RegisterNamespace::val
adds the hidden namespace to the scope when the compiler is trying to find GetMonday, and GetMonday returns a Days
which allows the compiler to find foo
.
Really you want the compiler to alter the scope by adding additional namespaces when it encounters a function from another namespace. However the compiler has already determined the types of the arguments by then, and actually needs them to work out other possible alternatives to the function.
#include <iostream>
namespace hidden {
enum RegisterNamespace { val };
enum Days {
Monday,
Tuesday
};
void foo(Days a , Days b){std::cout << "Called foo\n";}
Days GetMonday(RegisterNamespace a = val){return Days::Monday;}
Days GetTuesday(RegisterNamespace b = val){return Days::Tuesday;}
}
using namespace std;
#define UseNamespace(ns, x) do {using namespace ns; x;} while (0)
int main()
{
//with a macro
UseNamespace(hidden,hidden::foo(Monday, Tuesday));
{
//foo is found by argument dependent lookup
using hidden::Days;
foo(Days::Monday,Days::Tuesday);
}
{
using r = hidden::RegisterNamespace;
//foo and GetMonday / GetTuesday are all found by argument dependent lookup
foo(GetMonday(r::val),GetTuesday(r::val));
}
return 0;
}