Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we declare namespace aliases inside a class?

Tags:

c++

namespaces

It looks like it's impossible to declare a namespace alias inside a class; however we can do so at function-level (tested with g++ 4.3.4) :

namespace A
{
}

class C
{
  namespace N = A; // error: expected unqualified-id before `namespace'
};

class D
{
  void f();
};

void D::f()
{
  namespace N = A; // OK
}

Any idea why such a restriction exists ? This doesn't seem very consistent with typedefs which can be declared inside a class.

like image 545
Randall Flagg Avatar asked Feb 03 '11 09:02

Randall Flagg


People also ask

Can namespace be declared inside class?

Declaring a namespace within a class is invalid.

Can we create alias of a namespace?

You can also create an alias for a namespace or a type with a using alias directive.

Can a namespace have the same name as a class?

Inside a namespace, no two classes can have the same name.

What is namespace alias?

Namespace aliases allow the programmer to define an alternate name for a namespace. They are commonly used as a convenient shortcut for long or deeply-nested namespaces.

What is namespace alias in C++?

Namespace aliases. Namespace names need to be unique, which means that often they should not be too short. If the length of a name makes code difficult to read, or is tedious to type in a header file where using directives can’t be used, then you can make a namespace alias which serves as an abbreviation for the actual name.

Is it legal to declare a namespace within a class?

Declaring a class within a class is valid. (Nested classes) Declaring a namespace within a class is invalid. The question is: is there any good reason (other than c++ grammar/syntax problems) to forbid the declaration of a namespace within a class ?

What is the use of alias_name?

They are commonly used as a convenient shortcut for long or deeply-nested namespaces. The new alias alias_name provides an alternate method of accessing ns_name. alias_name must be a name not previously used. alias_name is valid for the duration of the scope in which it is introduced.

Can members of a named Namespace be defined outside the namespace?

Members of a named namespace can be defined outside the namespace in which they are declared by explicit qualification of the name being defined. However, the definition must appear after the point of declaration in a namespace that encloses the declaration's namespace.


1 Answers

According C++ standard 3.3.6

The following rules describe the scope of names declared in classes.

1) The potential scope of a name declared in a class consists not only of the declarative region following the name’s declarator, but also of all function bodies, default arguments, and constructor ctor- initializers in that class (including such things in nested classes). ...........

So you can declare only things from this list in class scope. Declaring anything else in class scope isn't valid. Not only namespace alliance, but also namespace. For example

class myClass
{
    //compilation error !!!
    namespace myNamespace
    {
    }
    using namespace std;//another compilation error
}

Edit:

Any idea why such a restriction exists ? This doesn't seem very consistent with typedefs which can be declared inside a class.

Because using typedefs in classes are very usefull (for example vector<int>::iterator), while for namespaces it's useless. Consider following code

class myClass
{
   namespce N=std;
};

//now let's use N
MyClass::N::vector<int> v;//don't you think, that this syntax is horrible, and useless?????

For comparison see what it is doing in function

void f()
{
    namespace bnu= boost::numeric::ublas;
    bnu::matrix<int> m;//and now we can use short name bnu
}

For class we can declare namespace alliance in cpp file and there is NO NEED to daclare it in class declaration.

like image 145
UmmaGumma Avatar answered Nov 15 '22 17:11

UmmaGumma