48

I expected to be able to use a namespace alias in a class declaration but get a compiler syntax error.

struct MyClass
{
    namespace abc = a_big_namespace;
    void fn() {
        abc::test();
    }
};

The only way I can get it to work is to put the alias in every function.

void fn() {
    namespace abc = a_big_namespace;
    abc::test();
}

Additionally I would like to be able to use the alias for function parameters. I haven't found a work-around for that.

void fn(abc::testType tt) {
    abc::test(tt);
}

Is there a way to do what I want?

EDIT: my solution

I found that I didn't need unnamed namespace for my particular problem and can simply do this:

namespace myspace
{
    namespace abc = a_big_namespace;

    struct MyClass
    {
       void fn(abc::testType tt) {
          abc::test(tt);
       }
    };
}

To switch to the other library, which is what my alias namespace refers to I just change the alias. This method even allows me to have the same class in a single file twice, each time refering to a different library. Thanks for all your help.

Ant
  • 1,668
  • 2
  • 18
  • 35

4 Answers4

48

Namespace alias in the class definition is illegal, as specified by the language specification.

Its allowed in only in namespace scope or function scope.

You can make an alias at the namespace scope. But this will create a permanent alias which can be used from other files as well. However, the solution is simple: you can use an unnamed namespace to prevent the alias (and therefore all symbols from the big namespace) from being visible from other files. This is how it can be done:

//MyFile.cpp
namespace myspace
{ 
    namespace   //this is unnamed namespace
    {
       namespace abc = a_big_namespace;     
    }
    struct MyClass 
    {
      void fn() 
      { 
         abc::test();  //don't worry, this will work!
      } 
    };
}

//OtherFile.cpp

myspace::abc::test(); //error - that means, prevention worked.

The alias is not visible from other files. When compiling OtherFile.cpp, GCC (4.5.0) says,

'myspace::abc' has not been declared

That proves the alias abc is visible only in MyFile.cpp. Thanks to the unnamed namespace.

Demo: http://www.ideone.com/2zyNI (though it doesn't demonstrate OtherFile concept since I cannot have more than one file at ideone.com)

Mael
  • 530
  • 3
  • 8
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    Many thanks Nawaz, I didn't realise the alias at class scope was illegal and I didn't known about the workaround. I had come across anonymous namespaces so I'll implement my code exactly as you suggest. Thanks again. – Ant May 19 '11 at 15:16
  • 5
    A caveat to the workaround described here: while it's true that these aliases are not visible in different cpp files, they _are_ visible in other header files. So, for example, if you apply this workaround in one header file, every other header file in your project can use that same alias, regardless of the surrounding namespace. You won't be able to assign that same alias to a different namespace in any other header file in the project, and you can only define a given alias once in a single header file. This is true for Visual Studio 2013, maybe not the same on other platforms. – Hoobajoob Mar 05 '15 at 23:11
  • " can make alias at namespace scope. But that will be make permanent alias which can be used from other files as well." - can you provide a reference for namespaces aliases being visible across compilation units? – Eric Jan 25 '18 at 00:55
  • 9
    I'm not sure how this is helpful. Namspace aliases in a cpp file are not seen anywhere else anyway. Anonymous namespaces AFAIK are for avoiding exporting/colliding symobls of functions or data with static linkage. – Catskul Feb 14 '18 at 18:45
  • What if `MyClass` needs to be defined in a header file? Is there a solution? – Tagli Jul 22 '21 at 11:11
  • @Tagli: _Why_ does it need to be defined in a header file? Why exactly/? – Nawaz Jul 25 '21 at 06:06
  • @Nawaz , because classes are generally defined in header files, so other cpp files can include and use them. Am I missing something? – Tagli Jul 25 '21 at 07:06
3

The scope of a namespace alias is a code block.

So you can put it in any code block.

BUT, you can't put it inside a class, because that will mean it's a member of the class.
A namespace alias can't be a member.

More about namespace aliases:

What is the scope of a namespace alias in C++?
Namespaces

Community
  • 1
  • 1
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • 1
    sorry but your answer is insatisfactory. classes and namespaces are super similar. yet we can have inner classes. we can even have inner typedefs, and inner usings for base methods. EVEN inner static asserts ! So no inner namespaces sounds like totally arbitrary judgment made by the comitee IMHO. – v.oddou Jun 14 '16 at 07:33
-1

It works if you declare the alias outside of the struct.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
-1

You can of course also put the alias outside the class:

namespace abc = a_big_namespace;     

struct MyClass {
    void fn()
    { abc::test(); } 
}; 
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 2
    My requirement was that the namespace be confined to the class because I'm using the same alias to reference two different libraries depending on which class I'm using. So yes, it works but it doesn't meet my requirements. Thanks for the idea though. – Ant May 19 '11 at 15:19