0

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?

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59

1 Answers1

1

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.

Community
  • 1
  • 1
Germán Diago
  • 7,473
  • 1
  • 36
  • 59
  • I said I understood the compiler error. The question was: Is there a way to mimic declaring `namespace SomeLongNamespaceHere{}` being empty. Then to give it a typedef sort of to speak so when I go and populate the namespace I can simply use `namespace slnh{ // populate here }` and all that is added through the shorter name actually belongs to the larger namespace so that I wouldn't have to type out the long version and neither would a user, with the added bonus of when hovering over top of the shorter version it will actually display the longer through intellisense... in the IDE. – Francis Cugler Feb 09 '17 at 06:56
  • No you cannot do that. For opening namespaces and populating you cannot use aliases. – Germán Diago Feb 09 '17 at 07:09
  • Diago oh okay; is there a Macro Way by using string substitution? – Francis Cugler Feb 09 '17 at 07:10
  • 1
    See my reply, but just not recommended. – Germán Diago Feb 09 '17 at 07:15
  • Oh okay no problem just wasn't sure; its been a long time since I've read about them or been introduced to them. I know what a namespace is and does and I have used them in simple terms, but wasn't sure about the syntax with aliasing them or string substitution. Thank You for the feedback and clarity of information. – Francis Cugler Feb 09 '17 at 09:57