[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/language/definition [Back]  [Original]

ODR (One Definition Rule - ) cppreference.com
cppreference.com

ODR (One Definition Rule - )

cppreference.com

, , , . , :

  • :
int f(int); // ,    f
  • extern (, extern "C") :
extern const int a; // ,    a
extern const int b = 1; //  b
  • ( C++17) :
struct S
{
    int n;               //  S::n
    static int i;        // ,    S::i
    inline static int x; //  S::x
};                       //  S
int S::i;                //  S::i
struct S
{
    static constexpr int x = 42; //  ,  S::x
};
constexpr int S::x; //  S::x,   
( C++17)
  • ( ):
struct S; // ,    S
class Y f(class T p); // ,    Y  T (  f  p)
enum Color : int; // ,    Color
( C++11)
template<typename T> // ,    T
  • , :
int f(int x); // ,    f  x
int f(int x)
{ //  f  x
     return x+a;
}
typedef S S2; // ,    S2 (S   )
using S2 = S; // ,    S2 (S   )
( C++11)
using N::d; // ,    d
  • ( ).
( C++17)
( C++11)
  • (" "):
extern template f<int, char>; // ,    f<int, char>
( C++11)
template<> struct A<int>; // ,    A<int>

asm , .

, , , , .

, .

, , , , ( C++20) ( , ).

, odr ( ), ( ) . , , , .

( C++17), , odr.

, , .

: , , , ( C++17), ( , ), :

( C++20)
  • ( )
  • ( ), ,
  • , ODR .
  • -, ( C++20) ,
( C++11)
  • , , , ( , )
  • (, extern "C")
  • const - ,
  • ,
  • , , odr,
  • , , odr,
( C++20)
  • , , .

, , . , .

: C ODR , extern , . C++ , , , : .cpp struct S { int x; };, .cpp struct S { int y; };, , , . .

ODR

,

  • odr, ( ) , ,
  • odr, , , ,
  • odr, .

, odr, - ; .

struct S
{
    static const int x = 0; //   
    //    ,    odr
};

const int& f(const int& r);

int n = b ? (1, S::x) // S::x    odr
          : f(S::x);  // S::x   odr:  

,

1) x ex odr, :
  • lvalue--rvalue x ,
  • x ( x ), , x , e, , lvalue--rvalue
struct S
{ 
    static const int x = 1;
}; //   lvalue--rvalue  S::x
                                      //   
int f()
{ 
    S::x;        //      odr  S::x
    return S::x; // ,     lvalue--rvalue,
                 //   odr  S::x
}
2) *this odr, this ( this -)
3) odr, .
( C++17)

E (, ) -, E, :

  • E, , E .
  • E (E1[E2]), , .
  • E E1.E2 E1. E2, , E1 .
  • E , , , .
  • E E1.*E2 E1.* E2, , E1 .
  • E, ((E1)), E1 .
  • E glvalue (E1?E2:E3, E2 E3 glvalue), E2 E3 .
  • E (E1,E2), E2 .
  • .
struct S
{
  static const int a = 1;
  static const int b = 2;
};
int f(bool x)
{
  return x ? S::a : S::b;
  // x    "x" (  ?),
  //    lvalue--rvalue,    
  //  x    ,  x  odr
  // S::a  S::b   lvalue,    " "
  //    glvalue
  //      lvalue--rvalue,  
  //    ,  S::a  S::b    odr
}
4) ODR :
  • odr, ( ) .
  • - odr, - ( vtable -)
  • odr .
  • odr .
  • T, U, odr U.
  • ( ) odr , .
  • odr, .

:

, odr , .

, constexpr , , , .

( C++11)

C++:

CWG 261 C++98
odr,
new delete

odr,
CWG 678 C++98
CWG 1472 C++98 ,
, odr,
lvalue--rvalue
CWG 1614 C++98 odr odr
CWG 1741 C++98 , lvalue rvalue
, odr
odr
CWG 1926 C++98
CWG 2242 C++98 , ODR const ,
ODR ;
CWG 2300 C++11 -

CWG 2353 C++98
,
CWG 2433 C++14

  • C++23 (ISO/IEC 14882:2023):
  • 6.3 [basic.def.odr]
  • C++20 (ISO/IEC 14882:2020):
  • 6.3 [basic.def.odr]
  • C++17 (ISO/IEC 14882:2017):
  • 6.2 [basic.def.odr]
  • C++14 (ISO/IEC 14882:2014):
  • 3.2 [basic.def.odr]
  • C++11 (ISO/IEC 14882:2011):
  • 3.2 [basic.def.odr]
  • C++03 (ISO/IEC 14882:2003):
  • 3.2 [basic.def.odr]
  • C++98 (ISO/IEC 14882:1998):
  • 3.2 [basic.def.odr]

Web Proxy Viewer  |  New URL  |  Original Page