unnamed-namespace Questions

6

Solved

Consider the following snippet: void Foo() // 1 { } namespace { void Foo() // 2 { } } int main() { Foo(); // Ambiguous. ::Foo(); // Calls the Foo in the global namespace (Foo #1). // I'm ...
Diffusive asked 9/9, 2010 at 2:21

1

Solved

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 = ...
Endomorphism asked 20/1, 2023 at 22:42

1

Solved

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 ...
Electroencephalogram asked 22/6, 2022 at 15:43

2

Solved

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?
Sectarianize asked 14/8, 2013 at 19:3

1

Solved

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 ...
Poetry asked 31/3, 2021 at 2:37

11

Solved

A feature of C++ is the ability to create unnamed (anonymous) namespaces, like so: namespace { int cannotAccessOutsideThisFile() { ... } } // namespace You would think that such a feature would...
Shufu asked 30/9, 2008 at 19:2

5

Solved

I was looking over some (C++) code and found something like this: //Foo.cpp namespace { void SomeHelperFunctionA() {} void SomeHelperFunctionB() {} void SomeHelperFunctionC() {} //etc......
Expiry asked 18/7, 2009 at 16:8

7

Solved

I've recently made a change stylistically and wanted to see how other c++ programmers felt about it and if there were any downsides to it. Essentially, when I needed a utility function which doesn...
Smoking asked 6/7, 2011 at 14:40

3

Solved

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...
Barbel asked 19/8, 2015 at 17:58

2

Solved

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 <iostream> int x = 10; namespace { int x = 20; }...
Lerma asked 16/9, 2016 at 12:40

3

Solved

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...
Granule asked 12/5, 2016 at 9:15

3

Solved

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...
Homemade asked 9/2, 2016 at 9:54

1

Solved

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....

5

Solved

Someone asserted on SO today that you should never use unnamed namespaces in header files. Normally this is correct, but I seem to remember once someone told me that one of the standard libraries u...
Vernavernacular asked 10/12, 2008 at 20:49

3

Solved

In C++ an unnamed namespace is equivalent to: namespace $$$$ { //something } using namespace $$$$; Where $$$$ is some kind of unique identifier. Unnamed namespaces are then useful for code that s...
Borsch asked 30/9, 2014 at 10:2

2

Solved

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...
Diplegia asked 14/5, 2014 at 10:33

2

Solved

How are unnamed namespaces superior to the static keyword?

3

What is the advantage of having a free function (in anonymous namespace and accessible only in a single source file) and sending all variables as parameters as opposed to having a private class mem...

2

Solved

I am porting code from Java to c++ and I'd like to replicate some anonymous functionalities. In file A.h I have : class A { private: int a; class AnonClass; friend class AnonClass; }; In fi...
Henchman asked 9/11, 2013 at 5:59

3

Solved

In C++, putting a function or a variable in an unnamed namespace makes its linkage internal, i. e. the same as declaring it static on a file-level, but idiomatic C++. What about an unnamed namespac...
Polyunsaturated asked 15/11, 2010 at 2:53

1

Solved

C++03 Standard 7.3.1.1 [namespace.unnamed] paragraph 1: (and C++11 Standard also use similar definition) An unnamed-namespace-definition behaves as if it were replaced by namespace unique { /* ...
Uneven asked 29/11, 2012 at 9:38

9

Solved

I need to define some constant strings that will be used only by one class. It looks like I have three options: Embed the strings directly into locations where they are used. Define them as priva...
Mccammon asked 17/3, 2010 at 20:19

3

Solved

namespace { int Foo (int a); } Is this code snippet legal? If so, can I reference Foo in anywhere, or only in a certain domain?
Caisson asked 6/10, 2011 at 6:39

2

Solved

There is following code: #include <iostream> using namespace std; namespace { int funkcja() { cout << "unnamed" << endl; return 0; } } int funkcja() { cout << "glo...
Hulsey asked 26/6, 2011 at 17:37

1

Solved

A recent thread on SO triggerred this. An unnamed namespace is considered to be equivalent to namespace unique { /* empty body */ } using namespace unique; namespace unique { namespace-body } ...

© 2022 - 2025 — McMap. All rights reserved.