tags:

views:

815

answers:

2

What is a "namespace alias" in C++? How is it used?

+4  A: 

A namespace alias is a convenient way of referring to a long namespace name by a different, shorter name.

As an example, say you wanted to use the numeric vectors from Boost's uBLAS without a using namespace directive. Stating the full namespace every time is cumbersome:

boost::numeric::ublas::vector<double> v;

Instead, you can define an alias for boost::numeric::ublas -- say we want to abbreviate this to just ublas:

namespace ublas = boost::numeric::ublas;


ublas::vector<double> v;
Martin B
I look forward to "what is an int".
anon
To possibly explain the downvotes, SO is not and never will be a replacement forv a good C++ textbook. The question you posed will be answered by any such book. And the SO "feature" of answering your own questions should not be used to provide paraphrases of such books.
anon
No offense taken... Just to explain why I did this: It was my understanding from Joel's comments in the podcast that even "entry-level" questions were fair game on SO, and that it was acceptable to ask a question and answer it yourself if that content wasn't on SO yet in an accessible form. But apparently, this is frowned upon?
Martin B
There is certainly an etiquette to answering your own question, to avoid irritation; in this case, it is pretty obvious that it never **was** a real question. For example, http://stackoverflow.com/questions/494927/stack-overflow-etiquette-for-answering-your-own-question
Marc Gravell
@Martin B: I don't agree that this is an entry level question - in fact there have been far more obvious questions asked in the past now with many votes. Having said that, people might feel you're simply trying to gain reputation for yourself. A way round this is to mark one or both of the question/answer as "community wiki". Personally I'd go with question as you and answer as community. If the question has merrit then you'll get ROI.
Richard Corden
The answer in Marc's link (ie. putting your answer into the question) is a good alternative too.
Richard Corden
Thanks -- didn't intend to offend or siphon off reputation. For what it's worth, at least I learned something, and the answer is in community wiki now.
Martin B
Michael Burr
I think the important question is whether the question is real" -- is it something that you have been asked? Is it something that people want to know? Is it something that hasn't already been asked and answered on SO?If you read the SO blog post about the R community posting and answering questions here, note that they picked the top X questions which their community *actually kept asking*, so it had real-world relevance. Taking random snippets of language-specific knowledge and posting them here seems less useful.
jalf
but when that is said, a significant part of the community simply sees red when they see someone answering his own question. I don't think it's such a bad thing (again, the R thing was a great idea), but *some* people think it's cheating.
jalf
A problem with this question might be that you'd only ever find it if you already knew the answer. If you don't know what a namespace alias is, you're not likely to find this question in the first place.
jalf
+1  A: 

Take a look here. It explains this briefly and gives an example.

erelender