Calling functions with the same name in a list of namespaces

You can't iterate through namespaces like that, but you could iterate through the different functions by explicitly listing them:

for (auto f : {one::a, two::a, three::a})
{
    f();
}

If you need to do this a lot though, I'd probably just write a function in global scope to call all the others. Or you could write a macro to shorthand the list above:

#define func_list(name) {one::name, two::name, three::name}

for (auto f : func_list(a))
{
    f();
}

It sort of depends on what you need to do in specific cases. However I would just suggest renaming them to be different or making differently-named wrapper functions in global scope.


If I have a full freedom on choosing mechanisms to use and c++14 compatible compiler I'd probably use tag dispatching + argument dependent lookup flavored with generic lambda to customize function call (choose the function to call afterwards):

#include <iostream>

namespace one {
struct Tag { } tag;
void a(Tag) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
void b(Tag) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
void c(Tag) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
}

namespace two {
struct Tag { } tag;
void a(Tag) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
void b(Tag) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
void c(Tag) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
}

namespace three {
struct Tag { } tag;
void a(Tag) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
void b(Tag) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
void c(Tag) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
}

template <class L, class... Ts>
void foreach(L&& l, Ts&& ...ts) {
    int execute[] = { 0, (l(ts), 1)... };
    static_cast<void>(execute);
}

int main() {
    foreach([](auto tag) { a(tag); }, one::tag, two::tag, three::tag);
}

output:

void one::a(one::Tag)
void two::a(two::Tag)
void three::a(three::Tag)

[live demo]

Tags:

C++