ABu
ABu

Reputation: 12259

Namespace alias for subnamespaces in local scopes

Consider that I want to call boost::algorithm::join as boost::join, but I don't want to import the subnamespace in the global namespace as:

namespace boost {
   using algorithm::boost;
};

Because I want to call boost::join with that syntax only in a local scope of an specific function:

void my_fun()
{
    namespace boost { // Doesn't allowed syntax in local scope
        using algorithm::join;
    }

    auto ret = boost::join(something...);
}

A different approach would be:

void my_fun()
{
   using boost::algorithm::join;
   auto ret = join(something...);
}

But this would provoke a ADL lookup, and I don't want to do such a lookup, because I know what method I'm calling: boost::algorithm::join.

Only, I'm trying to find the shorter boost::join way of calling it. What will be the correct syntax or idiom to create a "subnamespace" alias?

Upvotes: 1

Views: 156

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

You can't do this without polluting the Boost namespace, which is not yours to pollute.

Perhaps the following compromise can work for you?

namespace balgo = boost::algorithm;

Then use balgo::join.

So it's not a very descriptive name, but you're trying to make a short alias in a confined scope, right?

Otherwise, the affected region should be small enough that a simple join is clear and unambiguous (i.e. using namespace boost::algorithm); otherwise, just stick with the fully qualified name. Your team will thank you for it.

Upvotes: 2

Related Questions