Are template variables threads safe? Do they fit in the data segment?

I play with the new template variable function from C ++ 14 to get used to it (maybe this will happen soon, because it seems that some compilers have not fully implemented it).

Now I am wondering where each instance of the template variable lies. In the tests I have done so far, they seem to be initialized before any static data, so I wonder if they are placed in the data segment of the program. Let's see what I have tried so far, I have a class that prints information about construction and demolition:

struct squealer
{
    squealer(std::string a_name) : m_name(a_name) { std::cout << this << ' ' << m_name << ' ' << __PRETTY_FUNCTION__ << '\n'; }
    ~squealer() { std::cout << this << ' ' << m_name << ' ' << __PRETTY_FUNCTION__ << '\n'; }
    void f() {}
    const std::string m_name;
};

And the program in which some instances squealerare stored in local storage, static storage and as template variables is a program:

// static storage squealer
squealer s("\"static\"");

// template variable squealer
template <int i> squealer test(std::string(i, 'A'));

// function using a template variable squealer
void f() { test<1>.f(); }

int main(int argc, char **argv)
{
    // local storage squealer
    squealer ss("local");

    // using another template variable squealers
    test<2>.f();
    switch (argc)
    {
        case 1: test<3>.f(); break;
        case 2: test<4>.f(); break;
        case 3: test<5>.f(); break;
        case 4: test<6>.f(); break;
    }

    return 0;
}

, :

A squealer::squealer(std::string)
AA squealer::squealer(std::string)
AAA squealer::squealer(std::string)
AAAA squealer::squealer(std::string)
AAAAA squealer::squealer(std::string)
AAAAAA squealer::squealer(std::string)
"static" squealer::squealer(std::string)
local squealer::squealer(std::string)
local squealer::~squealer()
"static" squealer::~squealer()
AAAAAA squealer::~squealer()
AAAAA squealer::~squealer()
AAAA squealer::~squealer()
AAA squealer::~squealer()
AA squealer::~squealer()
A squealer::~squealer()

, squealer "static", ( ) local - ( ), : / ( f() ).

, , ? , .

: squealer ? n3376 §6.7 ( ):

, (3.6.2). , ; . , , , . , .

++ 11, squealer , , ?

.

+4
2

- , :

int foo() {
  static int bar = 42;
  return bar;
}

. , [basic.start.init] (3.6.2). , 2, :

(3.7.1) (3.7.2) (8.5) .

...

, [: . -end note] , , . (30.3), , . , . , . , .

squealer , squealer std::string, . ::s , test , " " test. test main, : , / ::s , , std::cout. , , : " , ".

+1

, [temp.inst]/12:

, , .

, , main().

+1

All Articles