Alex Rivera | Logout

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

Asked 2011-02-03T09:45:10.767
15

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.

Edit
Report

1 Answer

3

I disagree that namespace declaration within a class is completely useless. It would be useful to be able to declare enumerations within namespaces within classes. This would alow you to access the elements of different arrays with logical indices for the specific array.

class myClass
{
   private:
        namespace TransAndRotIdx {
            enum { U, V, W, P, Q, R }; };
        namespace RotIdx {
            enum { P, Q, R }; };
        double tr[6];
        double r[3];

    public:
        double getTranslationMag(void)
        {
            using namespace TransAndRotIdx;
            return sqrt(tr[U]*tr[U]+tr[V]*tr[V]+tr[W]*tr[W]);
        }
        double getRotationMag(void)
        {
            using namespace RotIdx;
            return sqrt(tr[P]*tr[P]+tr[Q]*tr[Q]+tr[R]*tr[R]);
        }
}
answered 2012-02-01T00:51:32.067

Your Answer