Home > OS >  Use all types defined inside a class
Use all types defined inside a class

Time:12-25

I have a struct X defined, which defines some types inside of it:

struct X {
    using A = int;
    using B = double;
    // ...
};

Now, it is possible to use these types outside of the class using typename, e.g.:

typename X::A a = 3;
typename X::B b = 3.5;

My question: If X was a namespace, then it would have been possible to use using namespace X to avoid typing typename X::... all of the time, but since X is a class, then we cannot do that. Is there any way to use the namespace X in order to avoid typing typename X::... all of the time?

Doing something like using namespace X does not seem to work. It is possible to use a singular type, using something like using A = X::A however I want a way to do this automatically for all types defined within a class.

Note that X may be a templated class, if that matters.

CodePudding user response:

Namespaces are designed for that purpose and gives more possibilities than classes in that respect.

But no, there is no such thing as using classspace X; A a = 3; (or equivalent) for class-name scope.

Considering that using namespace ... is often considered a bad practice, I doubt it will come soon.

  •  Tags:  
  • c
  • Related