Inline constructors and One Definition Rule

前端 未结 2 399
盖世英雄少女心
盖世英雄少女心 2020-12-19 21:28

Consider following source files 1.cpp

#include 

using namespace std;

struct X
{
    X()
    {
        cout << \"1\" << endl;
           


        
相关标签:
2条回答
  • 2020-12-19 22:20

    It is undefined behaviour (with no required diagnostic) if inlined functions (such as your class constructor) have different definitions in different translation units.

    0 讨论(0)
  • 2020-12-19 22:24

    This does violate ODR (3.2) - specifically that you can have more than one definition of an inline function, but those definitions must be identical (3.2/5) - and leads to undefined behavior, so anything may happen and the compiler/linker is not required to diagnose that. The most likely reason why you see that behavior is that function calls are inlined and do not participate in linking, so no link error is emitted.

    0 讨论(0)
提交回复
热议问题