static vs non-static variables in namespace
There are multiple meanings for static
in different contexts. In this particular context it means that the variable has internal linkage, and thus each translation unit that includes that header will have it's own copy of the variable.
Note that while this will silent the linker error, it will do so maintaining a separate foo::bar
variable for each one of the object files generated (changes will not be visible across different object files).
If you want a single variable, you should declare it as extern
in the header and provide a single definition in one translation unit.
Sharing static variables across files: namespace vs class
Yes:
//bar.h
namespace bar
{
extern int kode;
}
Outside of a class
or struct
, static
has a whole different meaning. It gives a symbol internal linkage. So if you declare the same variable as static
, you will actually get a different copy for all translation units, not a unique global.
Note that you'll need to initialize the variable once:
//bar.cpp
namespace bar
{
int kode = 1337;
}
What's the difference between a static member variable and a namespace variable?
Just in terms of the object you're declaring
The namespace declaration defines an object uniquely for the whole program. If you put that in a header file, including the header from two different source files would produce a multiple definition linker error.
The class static declaration asks the linker to share one object among all source files.
As for class vs. namespace
Classes and namespaces are completely different things. A class describes how to form an object. Class templates may also be used to form metaprograms. But a class should not be used merely to group things inside a scope, such that the class is never instantiated. That is the job of a namespace.
If you want to share the object among several sources, do this:
// Foo.h
namespace Foo {
extern int bar; // declaration
}
// Foo.cpp
namespace Foo {
int bar = 2; // definition
}
If the object is a constant, the definition is unnecessary as long as you never require bar
to have an address in memory.
static keyword useless in namespace scope?
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.
C++ namespace and static variables
- You don't need to define the variable as static, or in an anonymous namespace. However, if you're not using this object outside of the file it's defined in, it's a good idea, to reduce namespace pollution and speed up links (by reducing how many symbols need to be considered by the linker).
- If you declare a variable in an anonymous namespace, it will be effectively static. There's no need to actually make it static as well (although you can if you like). The advantage of anonymous namespaces is you can also define types (classes, structs, enums, typedefs) as well as static variables and functions.
do statics need a class?
Annex D (Compatibility features) [C++03]
D2: The use of the static keyword is deprecated when declaring objects in 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.
update
When you declare a variable as static
, it means that its scope is limited to the given translation unit only. Without static the scope is global.
When you declare a variable as static
inside a .h file (within or without namespace; doesn't matter), and include that header file in various .cpp files, the static variable becomes locally scoped to each of the .cpp files.
So now, every .cpp file that includes that header will have its own copy of that variable.
Without the static
keyword the compiler will generate only one copy of that variable, so as soon as you include the header file in multiple .cpp files the linker will complain about multiple definitions.
Related Topics
Get Size of Terminal Window (Rows/Columns)
How Do Memory_Order_Seq_Cst and Memory_Order_Acq_Rel Differ
Boost-Python How to Pass a C++ Class Instance to a Python Class
Can the 'Type' of a Lambda Expression Be Expressed
What Is a Nested Name Specifier
Comparing 3 Modern C++ Ways to Convert Integral Values to Strings
Returning Non-Const Reference from a Const Member Function
Pure/Const Function Attributes in Different Compilers
Why Is Volatile Deprecated in C++20
How to Build Boost Version 1.58.0 Using Visual Studio 2015 (Enterprise)
Performance Wise, How Fast Are Bitwise Operators VS. Normal Modulus
Project Error: Unknown Module(S) in Qt: Webkitwidgets
Std::Unique_Ptr with Derived Class
Edges on Polygon Outlines Not Always Correct
Pointer to Array of Unspecified Size "(*P)[]" Illegal in C++ But Legal in C