namespace for a class scope

T

toton

Hi,
Is it possible to have a class level namespace opening instead of
file level .

Something like this,
I have a long namespace like
namespace com { namespace my_company{ namespace my_project{ namespace
parser{
class my_parser{
};
}}}}

Now when I want to use this , I usually make a shortcut and use it,
with the assumption that I am working at my_project level, thus no
name resolution at that level is needed.

thus in the file,
namespace parser = com::my_company::my_project::parser;

Thus it is in the file scope.
Then use it as parser::my_parser in the header;
The same thing also can be done at function scope.

However, how this can be done in the class scope ?
like,

class my_parser_usage{
namespace parser = com::my_company::my_project::parser;
public:
parser::my_parser get_pasrer()const;
}
class my_second_parser_usage{
namespace parser = com::my_company::my_project::parser2;
public:
parser::my_parser get_pasrer()const;
}

Thanks
abir
 
A

Alan Johnson

toton said:
Hi,
Is it possible to have a class level namespace opening instead of
file level .

Something like this,
I have a long namespace like
namespace com { namespace my_company{ namespace my_project{ namespace
parser{
class my_parser{
};
}}}}

Now when I want to use this , I usually make a shortcut and use it,
with the assumption that I am working at my_project level, thus no
name resolution at that level is needed.

thus in the file,
namespace parser = com::my_company::my_project::parser;

Thus it is in the file scope.
Then use it as parser::my_parser in the header;
The same thing also can be done at function scope.

However, how this can be done in the class scope ?
like,

class my_parser_usage{
namespace parser = com::my_company::my_project::parser;
public:
parser::my_parser get_pasrer()const;
}
class my_second_parser_usage{
namespace parser = com::my_company::my_project::parser2;
public:
parser::my_parser get_pasrer()const;
}

Thanks
abir

As best I can tell from the grammar, a "member-specification" can't
contain a "namespace-alias-definition". That is, you can't use
namespace aliases in a class.

An alternative might by a typedef:
class my_parser_usage{
typedef com::my_company::my_project::parser::my_parser my_parser;
public:
my_parser get_parser() const;
};

Though it doesn't achieve quite the same effect you were going for.
 
G

Grizlyk

toton said:
However, how this can be done in the class scope ?
like,

class my_parser_usage{
namespace parser = com::my_company::my_project::parser;
public:
parser::my_parser get_pasrer()const;
}
class my_second_parser_usage{
namespace parser = com::my_company::my_project::parser2;
public:
parser::my_parser get_pasrer()const;
}

I am using dummy namespaces in the case

namespace X{
namespace parser = com::my_company::my_project::parser;

class my_parser_usage
{
public:
parser::my_parser get_pasrer()const;
};}
using X::my_parser_usage;
 
C

Craig Scott

Hi,
Is it possible to have a class level namespace opening instead of
file level .

Something like this,
I have a long namespace like
namespace com { namespace my_company{ namespace my_project{ namespace
parser{
class my_parser{
};

}}}}

Now when I want to use this , I usually make a shortcut and use it,
with the assumption that I am working at my_project level, thus no
name resolution at that level is needed.

thus in the file,
namespace parser = com::my_company::my_project::parser;

Thus it is in the file scope.
Then use it as parser::my_parser in the header;

Putting your namespace alias in the header would normally be something
to avoid. By putting it in a header, you are making it visible to
anyone who includes that header. Unless this is intentional and that
clients using the header expect this behavior, it would normally be
considered bad practice, bad design or both. Having said that, your
own situation may make this a perfectly valid choice, so take my
comments with a grain of salt if you like. ;)

A more dangerous habit would be to put the following in a header:

// maybe append ::parser to the following, but see below
using namespace com::my_company::my_project;

The above would bring my_project's contents into scope, which includes
parser, but also everything else in my_project. You could
append ::parser to the above using statement, but then there would be
no point in having the parser namespace at all. The reason I mention
this is to contrast it to what a namespace alias does for you. The
using statement makes the parser namespace redundant, whereas your
namespace alias makes parser's contents available via more than one
"scope path". This is not an error, but it does create ambiguity in
your interface. Again, if this is intentional, then fine, but normally
you wouldn't want to intentionally create such ambiguity when you have
a choice. In your own .cpp files, go nuts and use the namespace alias
as you want (that's what it is there for), but in header files, you
should give serious thought to whether you really want to do that to
your interface.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top