Questions tagged [unnamed-namespace]

An unnamed namespace in C++ restricts a set of declarations within an unnamed namespace block to the enclosing file. This is preferred by the standard over static functions, which fulfilled a similar purpose in C.

Unnamed namespaces are a C++ concept that refers to namespaces that are declared without specifying a name for them. An unnamed namespace would behave the same as a named namespace with a unique name.

Unnamed namespaces are a superior replacement for the static declaration of variables. They allow variables and functions to be visible within an entire translation unit, yet not visible externally. Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit.

More info: Unnamed Namespaces

26 questions
34
votes
2 answers

Nested unnamed namespace?

When using an unnamed namespace are there any issues if it is nested within another namespace? For example, is there any real difference between Foo1.cpp and Foo2.cpp in the following code: // Foo.h namespace Foo { void fooFunc(); } //…
Rob
  • 70,036
  • 53
  • 151
  • 189
10
votes
3 answers

Why should types be put in unnamed namespaces?

I understand the use of unnamed namespaces to make functions and variables have internal linkage. Unnamed namespaces are not used in header files; only source files. Types declared in a source file cannot be used outside. So what's the use of…
Francis Xavier
  • 205
  • 1
  • 10
9
votes
1 answer

Unnamed namespace Vs Global declaration

What is the difference in using unnamed namespace and global declaration? Is there any specific context for using these two? Can we access unnamed namespace components in external source files?
Kaushik
  • 982
  • 1
  • 13
  • 31
7
votes
2 answers

How would use of unnamed namespaces in headers cause ODR-violations?

In the Google C++ Style Guide, the Namespaces section states that "Use of unnamed namespaces in header files can easily cause violations of the C++ One Definition Rule (ODR)." I understand why not using unnamed namespaces in an implementation file…
boycy
  • 1,433
  • 12
  • 24
6
votes
3 answers

Unnecessary use of unnamed namespaces C++

I keep seeing code like this everywhere in my company: namespace { const MAX_LIMIT = 50; const std::string TOKEN = "Token"; } I am confused as of why you need an anonymous namespace here. On one hand, you want a local translation unit for…
FCR
  • 1,027
  • 8
  • 21
5
votes
2 answers

Accessing the variable inside anonymous namespace (c++)

I have following code and I don't know how can I access the x inside the anonymous namespace in this setting. Please tell me how? #include int x = 10; namespace { int x = 20; } int main(int x, char* y[]) { { int x = 30;…
Dongkyu Choi
  • 153
  • 8
5
votes
3 answers

External linkage for name inside unnamed namespace

According to the clause 3.5/4 of C++ Standard: An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage. Simultanously in paragraph 7.3.1.1 we have note 96): Although entities in an…
αλεχολυτ
  • 4,408
  • 1
  • 23
  • 62
4
votes
1 answer

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 #include namespace { std::function callback = [] { void theRealCallback(); //…
templatetypedef
  • 328,018
  • 92
  • 813
  • 992
4
votes
2 answers

How to access Unnamed namespace variable nested inside named namespace?

This question has alreday discussed in the link unnamed namespace within named namespace but no perfect answers were provided on how to access the variables of unnamed namespace nested under named namespace in case both variables are same Consider…
secretgenes
  • 1,151
  • 1
  • 17
  • 34
4
votes
1 answer

C++ namespace resolution

When I try to build this code: // foo.h namespace foo { namespace bar { void put(); } } #include "foo.h" namespace foo { namespace { template void put() { } } void bar::put() { …
Eric
  • 87,154
  • 48
  • 211
  • 332
3
votes
1 answer

Is static deprecated when ensuring availability between translation units?

From the following stackoverflow answer, the user says: It means that the variable is local to a translation unit (simply put, to a single source file), and cannot be accessed from outside it. This use of static is in fact deprecated in the…
Trevor Hickey
  • 31,728
  • 22
  • 131
  • 236
3
votes
3 answers

C++ class design: class or functions in unnamed namespace or private class method?

I am extending an existing class of new functionality and I at doubts about which design solution to use. There are several, each of them having pros and cons. My case is this: I have a file header which has a special format and I am going to read…
V.K.
  • 4,040
  • 1
  • 20
  • 54
3
votes
2 answers

Should unnamed namespace functions be avoided to reduce symbol table sizes?

I've heard it asserted that the use of unnamed namespaces in C++ to define functions and ensure that they cannot be called from outside the compilation unit in which they are defined is not good in very large code environments because they cause the…
WilliamKF
  • 36,283
  • 61
  • 170
  • 271
2
votes
1 answer

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 file-internal linkage. Does this mean unnamed…
John Z. Li
  • 1,515
  • 1
  • 9
  • 17
2
votes
0 answers

Instance of unnamed class in unnamed namespace

Using functors with parameters generally looks like that: // Definition: struct some_functor_with_params { int ref; explicit some_functor_with_params(int ref) : ref(ref) {} bool operator ()(int i) const {return i == ref;} }; //…
1
2