58 questions
13 votes
1 answer
549 views
Uniqueness of multiple unnamed namespaces within translation unit
I have recently come across this answer about forward declaration of the class in the unnamed namespace, and I was surprised that it indeed compiles and seems to work with Clang. I thought that every ...
0 votes
0 answers
55 views
What are use-cases and/or pros and cons of having a named namespace within an unnamed namespace?
The following "named namespace within unnamed namespace" scenario is allowed (as I understand it): namespace foo { namespace { namespace bar { int baz() { return 42; }...
0 votes
1 answer
49 views
Revert to unnamed namespace after importing another namespace within same scope
I have the following code from a textbook: namespace sally { void message( ) { std::cout << "Hello from Sally.\n"; } } namespace { void message( ) { std::...
6 votes
1 answer
197 views
How to access the unnamed namespace with scope resolution?
I have this: #include <iostream> using namespace std; // Variable created inside namespace namespace first { int val = 500; } namespace { int val = 400; } // Global variable //int val ...
0 votes
1 answer
87 views
Strange behavior (unnamed namespace with swscanf)
I have the next experimental code. That code fail with segmentation fault on the labeled line. If I take out the str2MxfKey function from anonymous namespace or give a some name to namespace, code ...
0 votes
0 answers
497 views
Should I put my using declarations in the unnamed namespace?
When implementing a function in CPP, I am used to put my helper functions in an anonymous namespace, so they don't pollute the global namespace outside the CPP file where they are defined. However, ...
6 votes
1 answer
163 views
Is declaring a friend which is forward-declared in an unnamed namespace an ODR-violation?
I've been using a dirty trick where I use unnamed namespaces to specify different behavior for each file (it is for unit-testing). It feels like it shouldn't be well-defined, but it works on every ...
-1 votes
1 answer
92 views
Should I convert C static function to private member function or free function in unnamed namespace?
I want to update some C legacy code to C++. Suppose I had something similar to this code in C: //my_struct.h typedef struct myStruct { //some members go here } myStruct; int f1(myStruct*); void f2(...
1 vote
0 answers
317 views
Is there a way to define a function template in a header and make it inaccessible to files who include the header?
I have a class template that looks like this: foo.h template<class C> class Foo { public: void memberFunc(); }; #include "foo.tpp" foo.tpp void Foo::memberFunc() { ... } ...
1 vote
1 answer
776 views
Are static or unnamed namespace still useful when header and implementation are separated?
As answered in this question, I learnt that the static keyword to the function means it can only be seen from the functions in that file. I think unnamed namespace can be used for the same purpose. ...
7 votes
1 answer
576 views
Do C++ modules make unnamed namespaces redundant?
C++20 introduced modules. Any symbol that is not exported in a module has module-internal linkage. While unnamed namespaces provide a mechanism to make definitions inside an unnamed namespace have ...
0 votes
0 answers
251 views
Redefinition error in different unnamed spaces
I have a redefinition problem with two functions in two different unnamed namespaces, which each is placed in two separate files. The first function grouping is in a one file: namespace { bool ...
1 vote
1 answer
829 views
non-inline namespace cannot be reopened as inline
I'm having an issue understanding compiler's complaint: namespace { } inline namespace { } gcc says inline namespace must be specified at initial definition and MSVC says what's in the title. My ...
4 votes
1 answer
1k views
Why do I get warnings both that a function is used but not defined and defined but not used?
I encountered an unusual pair of compiler warnings that seem mutually contradictory. Here's the code I've written: #include <functional> #include <iostream> namespace { std::function&...
1 vote
0 answers
46 views
How to define more than member of Un-named namespace outside?
When it comes to using namespaces it is really good thing to avoid name clashes. But the thing that matters me: Using anonymous namespaces. Here in my example for some reason I defined two un-named ...