ODR (One Definition Rule - )
cppreference.com
- :
int f(int); // , f
extern const int a; // , a
extern const int b = 1; // b
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 :
typedef S S2; // , S2 (S )
using S2 = S; // , S2 (S )
|
( C++11) |
using N::d; // , d
| ( C++17) | |
|
( C++11) |
- ( ).
- using- ( ).
extern template f<int, char>; // , f<int, char>
|
( C++11) |
template<> struct A<int>; // , A<int>
asm , .
( C++17), , odr.
| ( C++20) |
- ( )
- ( ), ,
- , ODR .
|
( C++11) |
- , , , ( , )
- (, extern "C")
-
const- , - ,
- , , 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, :
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
}
| ( C++17) |
E (, ) -, E, :
-
E, ,E. -
E(E1[E2]), , . -
EE1.E2E1. E2, ,E1. -
E, , , . -
EE1.*E2E1.* E2, ,E1. -
E, ((E1)),E1. -
Eglvalue (E1?E2:E3, E2 E3 glvalue),E2E3. -
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, odrU. - ( ) odr , .
- odr, .
| : , odr |
:
- , ( , , , operator new, ), , , , - .
- new .
- delete, .
- , , , . prvalue , . ( C++17)
, odr , .
| ( 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]