Why C++'s templated class doesn't break one definition rule?

后端 未结 3 2030

Maybe its lame question, But I don\'t get it! If I include or in multiple translation units (different .cpp) why it d

3条回答
  •  庸人自扰
    2021-01-12 00:27

    The standard has a special exception for templates that allows for duplication of functions that otherwise would violate ODR (such as functions with external linkage and non-inline member functions). from C++11 3.2/5:

    If D is a template and is defined in more than one translation unit, then the preceding requirements shall apply both 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.

提交回复
热议问题