Home > Mobile >  Bring nested name into scope in non-member function
Bring nested name into scope in non-member function

Time:04-15

I have a struct B that contains the declaration of a type anchor_point as a nested name. How can I bring anchor_point into scope in another function using the using-directive? I basically want to access the type as is without qualifiying it (just like from within a member function). I tried the following (see comments):

CODE

#include <memory>
#include <iostream>

struct B
{
    struct anchor_point
    {
        int a_;
    };
    int x;
    int y;
};

int main()
{
    // Here's what I want - doesn't compile
    // using B;
    // anchor_point ap1{5};

    // This gets laborious if there are dozens of types inside of B
    B::anchor_point ap2{5};

    // This is even more laborious
    using ap_t = B::anchor_point;
    ap_t ap3{5};

    std::cout << ap2.a_ << ", " << ap3.a_ << "\n";
}

The example is dumbed down but let's say I have a few dozens of those types declared inside the struct and I don't always want to type B::type, how can I do this?

CodePudding user response:

As you have shown you can do

using anchor_point = B::anchor_point;

repeated for every relevant member. This has to appear only once in the scope enclosing all your uses of the member that you want to cover.

There is no other way, in particular no equivalent to using namespace for namespaces which makes all members visible to unqualified name lookup.

  • Related