简体   繁体   中英

Does static const member have internal linkage?

Static members of class are compiled as global variable of class scope. How are compiled const static members, and static constexpr members? Does compiler for every .o file makes copy of this static member or it's done otherwise?

This is covered by C++14 [class.static.data]/5:

Static data members of a class in namespace scope have external linkage. A local class shall not have static data members.

"A class in namespace scope" means classes that aren't at block scope (aka. "local class"). For example this code:

void func()
{
    class C { static const int x = 5; };
}

is ill-formed.


To answer your question:

Does compiler for every .o file makes copy of this static member or it's done otherwise?

Typically, if the static member has a definition outside of the class there will be one copy of it, in the object file corresponding to the location of that definition, otherwise there will be none.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM