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
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
  • 41,123
  • 68
  • 193
  • 295
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;} }; //…
2
votes
1 answer

linkage of function with parameter from unnamed namespace

namespace { class A { } ; } void foo ( A ) // _Z3fooN12_GLOBAL__N_11AE { ; } the function's symbol, apparently, would refer to the name of A, which is a member of uniquely named namespace (due to this). what is the foo's linkage.?
Volodymyr Boiko
  • 1,533
  • 15
  • 29
2
votes
2 answers

How to call a variadic template helper in anonymous namespace?

In the code below, foo should be a function accessible by anyone, but foo_helper should not, which is why I've put it in an anonymous namespace. Obviously I'm leaving out include guards and includes in this example, but they are…
Leonhart231
  • 182
  • 2
  • 10
1
vote
0 answers

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 Foo { public: void memberFunc(); }; #include "foo.tpp" foo.tpp void Foo::memberFunc() { ... } Ignore the .tpp file, it's just something I do to give the illusion…
JensB
  • 839
  • 4
  • 19
1
vote
1 answer

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. However, usually, implementation and header files…
orematasaburo
  • 1,207
  • 10
  • 20
1
vote
1 answer

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 disarray comes from my expectation that two…
v.oddou
  • 6,476
  • 3
  • 32
  • 63
1
vote
0 answers

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 namespaces and accidently the two has a member…
WonFeiHong
  • 445
  • 4
  • 16
1
vote
0 answers

How do you get file information for variables in unnamed namespaces from PDB files?

I need to get the filenames from a PDB file which correspond to variables in unnamed namespaces. I'm using VS2012 and C++. I have put together a small sample of trivial code to illustrate the problem: // file: unnamed_namespace1.cpp Minimal test for…
1
vote
1 answer

Two unnamed namespaces, defined in the same declarative region

Consider the following code: #include namespace { int a = 5; } namespace { int a = 5; } int main() { int i=5; { std::cout << i; } } This code is invalid. It is because redefinition of a is occurred. But I…
user2953119
0
votes
0 answers

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 isValid(); } . . . namespace { bool isValid() …
swittuth
  • 77
  • 2
  • 5
0
votes
0 answers

Name conflict, unnamed namespace, class template

I have a class template Foo, deriving from FooBase. Foo.h: class FooBase { public: virtual ~FooBase() {} }; template class Foo : public FooBase { public: T t; }; Then, I have two translation units instantiating Foo with…
Boris Dalstein
  • 7,015
  • 4
  • 30
  • 59
0
votes
1 answer

Restricting access loudly to a global variable defined in a namespace

I have a small problem relating to namespaces, considering the following in a header file: namespace A { namespace B { void SetMemberValue(double value) { _member = value; } double FunctionThatUsesMember(double a) { return a *…
Casey
  • 10,297
  • 11
  • 59
  • 88
-1
votes
1 answer

Can I use unnamed namespaces instead of static variables inside functions?

I have browsed all the relevant questions about unnamed namespaces, yet I cannot see if and how they can be used to replace a static variable in this context: returnType dummyfun () { static int staticInt; // do something... };
Emerald Weapon
  • 2,392
  • 18
  • 29
1
2