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:
squealer s("\"static\"");
template <int i> squealer test(std::string(i, 'A'));
void f() { test<1>.f(); }
int main(int argc, char **argv)
{
squealer ss("local");
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 , , ?
.