By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,933 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,933 IT Pros & Developers. It's quick & easy.

namespace for a class scope

P: n/a
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

Feb 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
toton wrote:
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.

--
Alan Johnson
Feb 9 '07 #2

P: n/a
toton wrote:
>
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;

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 10 '07 #3

P: n/a
On Feb 9, 5:42 pm, "toton" <abirba...@gmail.comwrote:
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.

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia

Feb 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.