7

I have a class that I would like to reference in my header file, which is in a long chain of nested namespaces: MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass. I would like to use it under a different name, but not MyVeryLongNamedClass -- something shorter and more useful, like MyClass.

I could put using MySpaceA::MySpaceB::MySpaceC::MySpaceD in my header, but I do not want to import the whole namespace. I would prefer to have some kind of construction like

using MyClass = MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass

I know this is possible with name spaces, but I cannot seem to get it to work with classes.

Thank you very much for your help.

gt6989b
  • 4,125
  • 8
  • 46
  • 64

4 Answers4

21
typedef MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass MyClass;

For templates, you could use a template typedef:

template <typename T>
struct MyClass {
  typedef MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass<T> type;
};

Now you can refer to MyClass<T>::type instead of MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass<T>.

Community
  • 1
  • 1
Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
  • 1
    Ideally fix the namespace nesting as well but typedefs are great to save on typing and make code more readable especially with STL containers and other template classes that can end up looking like `std::vector, MyNamespace::MyClass>>` otherwise. – AJG85 May 12 '11 at 16:11
  • I tried `typedef`, but it does not work -- the compiler treats `MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass` as a declaration (since it is a template class), but I need it for a large number of different template parameters. so I cannot nail down one set of parameters for the typedef... – gt6989b May 12 '11 at 17:36
  • I guess the only real alternative is to define a new template to code in the old template, just changing the name... – gt6989b May 12 '11 at 17:39
2

Using cannot be used to alias classes - for that you need typedef. And do you really need those nested namespaces? The namespace feature of C++ was never intended to be an architectural mechanism - it was simply there to prevent name clashes. If you don't have clashes, which mostly you don't, don't use it!

  • This partially wrong, the `using` keyword can be used as a directive for namespaces like `using namespace std;` or as a declaration like `using std::vector;` however `using` should be avoided in header files in preference of explicitly saying `std::vector` – AJG85 May 12 '11 at 16:16
  • @AJG85 I meant it cannot be used to create new aliases for classes. –  May 12 '11 at 16:34
  • @unaperson I know, that's why I didn't downvote or anything just clarifying. – AJG85 May 12 '11 at 16:37
2

This seems to work for me

template <typename T>
struct MyClass : public MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass<T> {
};

Now you can just do

MyClass<int> foo;
MyClass<float> bar;
gman
  • 100,619
  • 31
  • 269
  • 393
1
using namespace MySpaceA::MySpaceB::MySpaceC::MySpaceD

Brings in the '...MySpaceD' namespace

using MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass

Only brings in the '..MyVeryLongNamedClass' class into your namespace.

You can 'alias' it with a typedef:

 #include <MyBigDeepNameSpaces.hh>

 namespace myPureNameSpace {
    typedef MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass MySomething_t ;
 }

Suggested Reading

C++ Coding Standards: 101 Rules, Guidelines, and Best Practices
By: Herb Sutter; Andrei Alexandrescu
Publisher: Addison-Wesley Professional
Pub. Date: October 25, 2004
Print ISBN-10: 0-321-11358-6
Print ISBN-13: 978-0-321-11358-0

Chapter 57

(stop namespace polution! Put out your cigarette!)

Andrew
  • 2,530
  • 16
  • 9
  • the first two solutions with `using` are not good. I explicitly explained in the question why I do not want them: one pollutes the namespace and another it still too long of a name. I tried `typedef`, but it does not work -- the compiler treats `MySpaceA::MySpaceB::MySpaceC::MySpaceD::MyVeryLongNamedClass` as a declaration (since it is a template class), but I need it for a large number of different template parameters. so I cannot nail down one set of parameters for the typedef... – gt6989b May 12 '11 at 17:34
  • You saved my day! Just using using (sic!), as in: using A::B::C; – Equilibrius Oct 06 '19 at 11:08