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

29 questions
37
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
  • 76,700
  • 56
  • 158
  • 197
12
votes
2 answers

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
  • 1,271
  • 2
  • 18
  • 35
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
  • 194
  • 1
  • 11
7
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,792
  • 1
  • 35
  • 71
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,473
  • 12
  • 25
6
votes
1 answer

How to access the unnamed namespace with scope resolution?

I have this: #include using namespace std; // Variable created inside namespace namespace first { int val = 500; } namespace { int val = 400; } // Global variable //int val = 100; int main() { // Local variable int val…
Johnz
  • 75
  • 4
6
votes
1 answer

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 major compiler released in the past six years. I…
Hedede
  • 1,133
  • 13
  • 27
6
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,893
  • 2
  • 12
  • 19
6
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;…
RyanC
  • 189
  • 1
  • 9
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,103
  • 10
  • 25
5
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,291
  • 1
  • 19
  • 39
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
  • 362,284
  • 104
  • 897
  • 1,065
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
  • 95,302
  • 53
  • 242
  • 374
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
  • 36,288
  • 32
  • 162
  • 271
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…
1
2