Typedef inside / outside anonymous namespace?

Are there any differences / preferences in the .cpp file anyway?

// file scope outside any namespace using X::SomeClass; typedef SomeClass::Buffer MyBuf; 

V / s

 namespace { // anonymous using X::SomeClass; typedef SomeClass::Buffer MyBuf; } 
+6
c ++ namespaces typedef using directive
source share
2 answers

I would say that the second use is rather unusual, at least in the code that I have seen so far (and I have seen quite a lot of C ++ code). Could you explain what the second method explains?

Usually you use an anonymous namespace in the C ++ implementation file to achieve the same thing as "static" in C (or C ++, but we will mask it), namely limiting the visibility of characters to the current translation unit. Typedef does not actually create characters that are exported for the linker to see, because they do not create anything β€œspecific” in the sense of anything specific that you could associate with.

My recommendation? I would go with the first notation. The second adds an unnecessary complication and, in my opinion, does not buy anything.

+7
source share

It makes no sense to specify typedefs in anonymous namespaces. The main use for anonymous namespaces is to avoid collision of characters between translation units by placing definitions with external links in them.

+5
source share

All Articles