开发者

static keyword useless in namespace scope?

namespace N
{
   static int x = 5;
}

What could be the importance/use-cases of decl开发者_如何学Pythonaring having a static variable at namespace scope?


static variable at namespace scope (global or otherwise) has internal linkage. That means, it cannot be accessed from other translation units. It is internal to the translation unit in which it is declared.


Annex D (Compatibility features) [C++03]

D2: The use of the static keyword is deprecated when declaring objects in namespace scope.

Use unnamed namespaces instead as mentioned in this post.

static keyword imparts internal linkage to variables/objects in C as well as in C++ in namespace scope as others have mentioned in their posts.

P.S: Thie feature has been undeprecated as per the latest draft (n3290). In n3225 §7.3.1.1/2 is present but striked out.


I agree with Nawaz's answer: static keyword is not totally useless in namespaces : it defines that the variable is of internal linkage to the translation unit.

For example : header.h

namespace test{
    static int i = 5;//definition is here.And there will be no multiple definition!
    void set_i();
    void print_i();
}

header.cpp

#include "header.h"
#include "iostream"
void test::set_i()
{
    i = 10;
    return;
}

void test:print_i()
{
    using namespace std;
    cout << "print_i is " << i << endl;
    return;
}

main.cpp

#include "header.h"
#include "iostream"
using std::cout;
int main()
{
    test::i = 20;
    test::set_i();
    cout << "i is " << test::i << endl; 
    test::print_i();
    return 0;
}

Use g++ -std=c++11 header.cpp main.cpp to compile it, and you won't get multiple definition error.If you remove the static keyword and compile it, it is definitely multiple definition error. And please do run the program and observe the result and you may be surprised.

static keyword makes every cpp implementation file(translation unit) which includes the interface header that contains namespace declaration has a internal-linkage copy of the static variable. So even you define that variable in namespace, if it is static keyworded, there will be no multiple definition error.(The same for const with no extern preceding it which define variable of internal linkage, and that's how macros can be discarded in C++) So the saying that variables defined in namespace are implicitly static is wrong and static are not totally useless in namespace. Because every translation unit have one copy of that variable, space is consumed.

However, unnamed namespace can make class declaration inside it inaccessible from other translation unit while static keyword can not "attribute" a class, so that is one advantage of unnamed namespace. Besides, you can use unnamed namespace in nested namespace for variable access restriction. Unnamed namespace are designed for protecting locality rather than offering interface.


C++ Standard §7.3.1.1/2:

The use of the static keyword is deprecated when declaring objects in a namespace scope (see annex D); the unnamed-namespace provides a superior alternative.

Unless the unnamed namespace provides a superior alternative in the future Standard it will be undeprecated to achieve C compatibility.


Same as that of declaring a static in a global namespace but just local to a particular namespace.


What others already said, with an additional subtlety: static introduces internal linkage, and anonymous namespaces do not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜