I have a function in my namespace, ns::foo, whose job is to dispatch an invocation of foo using argument-dependent lookup:
namespace ns
{
template<typename T>
void foo(T x)
{
// call foo through ADL
foo(x);
}
}
I want clients to be able to call foo without having to manually instantiate it, i.e.:
bar x;
ns::foo(x);
Not
ns::foo<bar>(x);
The problem of course is that ns::foo is recursive if there is no better match for foo than ns::foo.
I don’t wish to give ns::foo a different name, so is there any way to remove it from the overload set inside itself?
If the
footo where you want to dispatch is not in thensnamespace, then this should work:The trick is that the call to
foofrom_foowill not considerns::foo, because it is not in an argument-dependent namespace. Unless the type ofxhappens to be innsof course, but then you have a recursion by definition.UPDATE: You have to put this code just after the definition of
namespace ns:There is no recursion because the
helper::_foofunction cannot call thetemplate foobecause it is still not defined.