static keyword useless in namespace scope?

后端 未结 6 1847
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 12:30
namespace N
{
   static int x = 5;
}

What could be the importance/use-cases of declaring having a static variable at namespace scope?

相关标签:
6条回答
  • 2021-01-01 12:44

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

    0 讨论(0)
  • 2021-01-01 12:46

    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.

    0 讨论(0)
  • 2021-01-01 12:46

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

    0 讨论(0)
  • 2021-01-01 13:01

    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.

    0 讨论(0)
  • 2021-01-01 13:04

    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.

    0 讨论(0)
  • 2021-01-01 13:06

    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.

    0 讨论(0)
提交回复
热议问题