One Definition Rule

One Definition Rule

The One Definition Rule (ODR) is an important concept in the C++ programming language. It's defined in the ISO C++ Standard(ISO/IEC 14882) 2003, at section 3.2.

Contents

Summary

In short the ODR states that:

  1. In any translation unit, a template, type, function, or object can have no more than one definition. Some of these can have any number of declarations. A definition provides an instance.
  2. In the entire program, an object or non-inline function cannot have more than one definition; if an object or function is used, it must have exactly one definition. You can declare an object or function that is never used, in which case you don't have to provide a definition. In no event can there be more than one definition.
  3. Some things, like types, templates, and extern inline functions, can be defined in more than one translation unit. For a given entity, each definition must be the same. Non-extern objects and functions in different translation units are different entities, even if their names and types are the same.

Some violations of the ODR must be diagnosed by the compiler. Other violations, particularly those that span translation units, are not required to be diagnosed.[1]

Examples

In general, a translation unit shall contain no more than one definition of any class type. In this example, two definitions of the class type C occur in the same translation unit. This typically occurs if a header file is included twice by the same source file without appropriate header guards.

class C {}; // first definition of C
class C {}; // error, second definition of C

In the following, forming a pointer to S or defining a function taking a reference to S are examples of legal constructs, because they do not require the type of S to be complete. Therefore, a definition is not required.[2]

Defining an object of type S, a function taking an argument of type S, or using S in a sizeof expression are examples of contexts where S must be complete, and therefore require a definition.[2]

struct S;   // declaration of S
S * p;      // ok, no definition required
void f(S&); // ok, no definition required
void f(S);  // ok, no definition required 
S f();      // ok, no definition required  
 
S s;        // error, definition required
sizeof(S);  // error, definition required

More than one definition

In certain cases, there can be more than one definition of a type or a template. A program consisting of multiple header files and source files will typically have more than one definition of a type, but not more than one definition per translation unit.

If a program contains more than one definition of a type, then each definition must be equivalent.[3]

Definitions of static const data members

In pre-standard C++, all static data members required a definition outside of their class. However, during the C++ standardization process it was decided to lift this requirement for static const integral members. The intent was to allow uses such as:

struct C {
  static const int N = 10;
};
char data[C::N]; // N "used" without out-of-class definition

without a namespace scope definition for N.

Nevertheless, the wording of the 1998 C++ standard still required a definition if the member was used in the program.[4] This included the member appearing anywhere except as the operand to sizeof or typeid, effectively making the above ill-formed.[5]

This was identified as a defect, and the wording was adjusted to allow such a member to appear anywhere a constant expression is required, without requiring an out-of-class definition. This includes array bounds, case expressions, static member initializers, and nontype template arguments.[6]

struct C {
  static const int N = 10;
  static const int U = N; // Legal per C++03
};
 
char data[C::N]; // Legal per C++03
 
template<int> struct D;
 
template<> struct D<C::N> {}; // Legal per C++03

However, using a static const integral member anywhere except where an integral constant-expression is required requires a definition:[7]

struct C {
  static const int N = 10;
};
 
int main() {
  int i = C::N; // ill-formed, definition of C::N required
}

This requirement has been relaxed in the current C++ standard, C++11.[7]

See also

References

  1. ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §3.2 One definition rule [basic.def.odr] para. 3
  2. ^ a b ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §3.2 One definition rule [basic.def.odr] para. 4
  3. ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §3.2 One definition rule [basic.def.odr] para. 5
  4. ^ ISO/IEC (1998). ISO/IEC 14882:1998(E): Programming Languages - C++ §9.4.2 Static data members [class.static.data] para. 4
  5. ^ ISO/IEC (1998). ISO/IEC 14882:1998(E): Programming Languages - C++ §3.2 One definition rule [basic.def.odr] para. 2
  6. ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §5.19 Constant expressions [expr.const] para. 1
  7. ^ a b "When is a definition of a static data member required?". WG21. http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#454. Retrieved 2009-04-15. 

Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • One-drop rule — The one drop rule is a historical colloquial term in the United States for the social classification as black of individuals with any African ancestry; meaning any person with one drop of black blood was considered as black. The principle was an… …   Wikipedia

  • Definition — For other uses, see Definition (disambiguation). A definition is a passage that explains the meaning of a term (a word, phrase or other set of symbols), or a type of thing. The term to be defined is the definiendum. A term may have many different …   Wikipedia

  • One Life to Live — Title card (2004–present) Genre Soap opera Created by Agnes Nixon …   Wikipedia

  • Rule of law — The rule of law, in its most basic form, is the principle that no one is above the law. Thomas Paine stated in his pamphlet Common Sense (1776): For as in absolute governments the king is law, so in free countries the law ought to be king; and… …   Wikipedia

  • Definition of terrorism — terrorismThe word terrorism is politically and emotionally charged,Hoffman, Bruce Inside Terrorism Columbia University Press 1998 ISBN 0 231 11468 0. Page 32. See review in The New York Times [http://www.nytimes.com/books/first/h/hoffman… …   Wikipedia

  • One-hit wonder — OHW redirects here. For the facility known as Ontario Hospital, Whitby , see Whitby Psychiatric Hospital. For other uses, see One Hit Wonder (disambiguation). A one hit wonder is a person or act known mainly for only a single success. The term is …   Wikipedia

  • Rule of product — In combinatorics, the rule of product or multiplication principle is a basic counting principle (a.k.a. the fundamental principle of counting). Stated simply, it is the idea that if we have a ways of doing something and b ways of doing another… …   Wikipedia

  • One-China policy — The One China policy (simplified Chinese: 一个中国; traditional Chinese: 一個中國 政策 ; pinyin: yī gè Zhōngguó) refers to the policy or view that there is only one state called China , despite the existence of two governments that claim to be China …   Wikipedia

  • Rule of 1/1000th common ancestry — The Rule of 1/1000th common ancestry is a criterion used to create meaningful family groupings. It was first adopted by Lawrence Kestenbaum to determine which individuals should be included with specific political families on the Political… …   Wikipedia

  • One percenter (Australian rules football) — A one percenter (1%er) is a statistic kept in Australian football, and relates to a variety of actions (or checking skills if using North American terms see ice hockey) which benefit the team, but are infrequent or defensive. Contents 1 History 2 …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”