Anonymous namespaces - working or what?

I wrote a little test snippet to clarify some things for myself.
Everywhere I looked, it says anonymous namespaces should be recognized by the compilers.
Well, mine doesn't agree.

This is the test code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Test #1: renamed namespaces have block scope.
// Uncommenting line #26 gives compiler error.

#include <iostream>

namespace Names1 {
	int m = 1;
	float x = 3.14F;
}

namespace Names2 {
	int m = 2;
	float x = 6.28F;
}

float op (int a, float b) {
	return a * b;
}

int main () {
	using namespace Names2;
	{
		namespace N1 = Names1;
		std::cout << op(N1::m, N1::x) << std::endl;
	}
//	std::cout << op(N1::m, N1::x) << std::endl;
	{
		namespace { int m = -1; float x = 3.14F; }
		std::cout << op(m, x) << std::endl;
	}

	{
		std::cout << op(m, x) << std::endl;
	}

	return 0;
}


and this is what I get when I try to compile it:

1
2
3
4
5
6
user:~$ g++ -std=c++11 -o test Namespace_renaming_scope_demo.cpp 
Namespace_renaming_scope_demo.cpp: In function ‘int main()’:
Namespace_renaming_scope_demo.cpp:28:13: error: expected identifier before ‘{’ token
   namespace { int m = -1; float x = 3.14F; }
             ^
user:~$


Is this some super new feature or what?
Or maybe I'm supposed to use some other fancy compiler option in the command line?
I'm using g++ version 4.9.1
Hello cristian c,

Look at the error message it tells you everything you need to know.

On line 28 you use the keyword "namespace", but fail to give it a token, i.e., variable name to use as you did on line 23. I admit I am not an expert when it comes to creating a name space, but this seems to be the most logical answer.

Hope that helps,

Andy
Is this some super new feature or what?

No. An unnamed namespace is a short hand for:
1
2
3
4
namespace __unique_042aff9 {
    //...
}
using namespace __unique_042aff9;

The issue is simply that you cannot create namespaces inside function bodies.
Hence you cannot create unnamed namespaces there either.

Contrary to Handy Andy's statement, the error message is misleading, because if you had given the namespace a name you would've gotten a 'namespace' definition not allowed here error which tells you the real culprit here.
Last edited on
@Handy Andy

What do you mean??
That's the whole point of anonymous namespaces: they are not supposed to have a name!
So there shouldn't be anything between "namespace" and "{"
At least that's what I read in all tutorials...
It's true, on cplusplus.com anonymous namespaces are not even mentioned (why would that be?) but lots of other places do mention them.

Look:
http://en.cppreference.com/w/cpp/language/namespace
https://www.cprogramming.com/tutorial/namespaces.html
https://msdn.microsoft.com/en-us/library/5cb46ksf.aspx
http://www.cplusplus.com/forum/general/39196/
https://stackoverflow.com/questions/357404/why-are-unnamed-namespaces-used-and-what-are-their-benefits
Last edited on
@Ihatov

My reply above was actually for Andy, but it seems we posted more or less simultaneously, your post beating mine by some 1/4 seconds or something... :)

Thanks for this tip, this is really tricky !
No tutorial I've read mentioned this. I wasn't aware I can't declare a namespace inside a block.
I just assumed since I can rename one inside a block, declaring one would work too...

I'm going to add this to my C++ course notes...
@ Ihatov,

Thank you for the correction.

Andy
Topic archived. No new replies allowed.