Francis Cugler
Francis Cugler

Reputation: 7905

C++ Confusion about namespaces and aliasing: no nesting and no using directives

I'm not trying to nest a namespace, I'm trying not to use a using directive in my header. I'm trying to alias a namespace 'name' with another name. In my header I have a global empty namespace with a full name. I want to assign another namespace to this name and then populate it with my source. I'm not sure what the appropriate syntax is for doing such a thing. Here is an example of what I have in my header file.

#ifndef SOME_HEADER_H
#define SOME_HEADER_H

namespace SomeLongNamespaceHere {} // Empty Global
namespace SLNH = SomeLongNamespaceHere; // Alias
namespace slnh = SLNH; // another lowercase alias

namespace slnh { // Compiler Error:
    // My Declarations Here!
    class foo{};    
}

#endif // SOME_HEADER_H

1>------ Build started: Project: FileTester, Configuration: Debug Win32 ------
1>  include.cpp
1>c:\users\skilz80\documents\visual studio 2015\projects\filetester\filetester\include.h(8): error C2757: 'mfbid': a symbol with this name already exists and therefore this name cannot be used as a namespace name
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I understand the compiler error and that is not the issue or the problem.


However when I do this:

namespace slnh{}

namespace slnh{
    class foo{};
}

It compiles just fine. I'm not sure if I'm misunderstanding what a namespace alias exactly is and does or if I'm not using the proper syntax.


I am trying to use the shorter version in place of the longer version so that the user can just use the shorter version directly and when they hover over the shorter namespace it will automatically resolve back to the longer namespace and can be seen through features such as MSVS's intellisense or something similar. How can I achieve or mimic the behavior that I have described above?

Upvotes: 0

Views: 416

Answers (1)

Germán Diago
Germán Diago

Reputation: 7673

You can reopen namespaces as many times as you want that is why this works:

namespace slnh{}

namespace slnh{
    class foo{};
}

But you cannot redeclare a namespace with the same name of an alias, it is a conflict:

namespace SomeLongNamespaceHere {} // Empty Global
namespace SLNH = SomeLongNamespaceHere; // Alias
namespace slnh = SLNH; // another lowercase alias

//This namespace clashes with the alias above.
namespace slnh { // Compiler Error:
// My Declarations Here!
    class foo{};    
}

It does not work because you are associating the same name to a namespace and to a namespace alias. This would clash AFAIK.

EDIT: You cannot in anyway open a namespace with an aliased name. So it cannot be done.

Maybe a possible workaround (not recommended at all) is to use a shorter namespace and put a using directive:

namespace SomeLongNamespace {
    using namespace SLN;
}

namespace SLN {
   //Populate here
   class MyClass {};
}


SLN::MyClass ...; //works 
SomeLongNamespace::MyClass ...; //works

Another ugly workaround would be using macros (not recommended either):

#define SLN SomeLongNamespace

This can be bad for just saving a few keystrokes and macros are evil so do not do it.

Upvotes: 1

Related Questions