Non-Static Data Members and One Definition Rule

Premise

According to the Rule of Definition , as stated in C ++ 14 Standard , I can have a definition of the same class in each translation unit if I follow the rules in 3.2.6. This means that the following program will be legal:

//a_1.cpp
class A {                      //definition of A
    int a;                     //definition of A::a
    static int b;              //declaration of A::b
    int foo();                 //declaration of A::foo();
    int boo(){ return 42; };   //definition of A::boo() implicity inlined 
};

//a_2.cpp
class A {                      //definition of A
    int a;                     //definition of A::a
    static int b;              //declaration of A::b
    int foo();                 //declaration of A::foo();
    int boo(){ return 42; };   //definition of A::boo() implicity inlined 
};

If I try to define bor foo(), I will limit myself to just one definition in the whole program, which, it seems to me, is connected with the statement in 3.2.4:

Each program must contain exactly one definition of each non-built-in function or variable used by odr in this program; no diagnostics required.

For this reason, the following program is not correctly formed:

//a_1.cpp
class A {                      //definition of A
    int a;                     //definition of A::a
    static int b;              //declaration of A::b
    int foo();                 //declaration of A::foo();
    int boo(){ return 42; };   //definition of A::boo() implicity inlined 
};
int A::b;

//a_2.cpp
class A {                      //definition of A
    int a;                     //definition of A::a
    static int b;              //declaration of A::b
    int foo();                 //declaration of A::foo();
    int boo(){ return 42; };   //definition of A::boo() implicitly inlined 
};
int A::b;

, foo() .

, , boo() ( ), 3.2.4 , , 3.2.6:

( 9), (7.2), (7.1.2), ( 14), (14.5.6), (14.5.1.3), - (14.5.1.1) (14.7, 14.5.5) , , .

, 3.2.6 , , ( boo()) .

a? , a ( , , ), , -, 3.2.4, 3.2.6. , , - ?

Edit

, , a , , , C + +14 , 3.2.2:

struct X { // defines X
    int x; // defines non-static data member x
    static int y; // declares static data member y
    X(): x(0) { } // defines a constructor of X
};

, , , .

+4
2

[basic.def.odr]/1:

, , , .

[basic]/6:

, .

, , , , , .

+3

a . a. .

, include , , .

0

Source: https://habr.com/ru/post/1662782/


All Articles