Static variable inside template function

前端 未结 7 1909
一向
一向 2020-12-14 07:10

In C++, if you define this function in header.hpp

void incAndShow()
{
  static int myStaticVar = 0;
  std::cout << ++myStaticVar << \" \" <<         


        
7条回答
  •  有刺的猬
    2020-12-14 07:24

    You can rely on this. The ODR (One Definition Rule) says at 3.2/5 in the Standard, where D stands for the non-static function template (cursive font by me)

    If D is a template, and is defined in more than one translation unit, then the last four requirements from the list above shall apply to names from the template’s enclosing scope used in the template definition (14.6.3), and also to dependent names at the point of instantiation (14.6.2). If the definitions of D satisfy all these requirements, then the program shall behave as if there were a single definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined.

    Of the last four requirements, the two most important are roughly

    • each definition of D shall consist of the same sequence of tokens
    • names in each definition shall refer to the same things ("entities")

    Edit

    I figure that this alone is not sufficient to guarantee that your static variables in the different instantiations are all the same. The above only guarantees that the multiple definitions of the template is valid. It doesn't say something about the specializations generated from it.

    This is where linkage kicks in. If the name of a function template specialization (which is a function) has external linkage (3.5/4), then a name that refers to such a specialization refers to the same function. For a template that was declared static, functions instantiated from it have internal linkage, because of

    Entities generated from a template with internal linkage are distinct from all entities generated in other translation units. -- 14/4

    A name having namespace scope (3.3.6) has internal linkage if it is the name of [...] an object, reference, function or function template that is explicitly declared static -- 3.5/3

    If the function template wasn't declared with static, then it has extern linkage (that, by the way, is also the reason that we have to follow the ODR at all. Otherwise, D would not be multiply defined at all!). This can be derived from 14/4 (together with 3.5/3)

    A non-member function template can have internal linkage; any other template name shall have external linkage. -- 14/4.

    Finally, we come to the conclusion that a function template specialization generated from a function template with external linkage has itself external linkage by 3.5/4:

    A name having namespace scope has external linkage if it is the name of [...] a function, unless it has internal linkage -- 3.5/4

    And when it has internal linkage was explained by 3.5/3 for functions provided by explicit specializations, and 14/4 for generated specializations (template instantiations). Since your template name has external linkage, all your specializations have external linkage: If you use their name (incAndShow) from different translation units, they will refer to the same functions, which means your static objects will be the same in each occasion.

提交回复
热议问题