Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unnamed namespace

Tags:

c++

namespaces

What does it exactly mean when the Standard states

$7.3.1.1/2 - "The use of the static keyword is deprecated when declaring variables in a namespace scope (see annex D); the unnamed-namespace provides a superior alternative."

I have referred this but it does not cover what I am looking for.

Is there an example where the superiority is clearly demonstrated.

NB: I know about how unnamed namespaces can make extern variables visible in the translation unit and yet hide them from other translation units. But the point of this post is about 'static namespace scope' names (e.g global static variables)

like image 862
Chubsdad Avatar asked Nov 18 '10 09:11

Chubsdad


People also ask

What is an unnamed namespace?

Unnamed Namespaces The name of the namespace is uniquely generated by the compiler. The unnamed namespaces you have created will only be accessible within the file you created it in. Unnamed namespaces are the replacement for the static declaration of variables.

Is unnamed namespace global?

The point of an unnamed namespace is to provide a unique namespace within a translation unit (= a source file) without requiring an explicit prefix. This allows you to guarantee that your global names won't clash with other, equal global names in other translation units.

Do not define an unnamed namespace in a header file?

The analyzer detected an anonymous namespace declared in the header file. Such a header file creates copies of symbols with internal linkage in each translation unit that includes this header file. This leads to object files bloat, which may be unwanted behavior.

What do you mean by namespace?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.


1 Answers

What does it exactly mean?

Technically deprecated means that a future standard may remove the feature.

In practice that isn't going to happen, because of the need to support old code.

So in practice it means, "strongly discouraged".

Example of superiority of unnamed namespace

An unnamed namespace is generally superior because what you have in that namespace can have external linkage.

In C++98 external linkage is necessary for things that can be template parameters, e.g., if you want to templatize on a char const*, it must be pointer to char that has external linkage.

#include <iostream>

// Compile with "-D LINKAGE=static" to see problem with "static"
#ifndef LINKAGE
#   define LINKAGE extern
#endif

template< char const* s >
void foo()
{
    std::cout << s << std::endl;
}

namespace {
    LINKAGE char const message[] = "Hello, world!";
}  // namespace anon

int main()
{
    foo<message>();
}

That said, it's a bit inconsistent that static isn't also deprecated for functions.

like image 160
Cheers and hth. - Alf Avatar answered Oct 23 '22 19:10

Cheers and hth. - Alf