Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is One Definition Rule in C++?

Tags:

c++

definition

What exactly does One Definition Rule in C++ say? The only trustworthy occurence I can find is in The C++ Programming Language, 3rd. ed., P. 9.2.3. Is there any official definition of the rule except that?

like image 530
sharptooth Avatar asked Nov 16 '10 08:11

sharptooth


People also ask

What does ODR-used mean?

10. +1 for the succinct opening sentence: "In plain word, odr-used means something(variable or function) is used in a context where the definition of it must be present."

What is ODR violation?

If an object, a reference or a function is odr-used, its definition must exist somewhere in the program; a violation of that is usually a link-time error. struct S { static const int x = 0; // static data member // a definition outside of class is required if it is odr-used }; const int& f(const int& r); int n = b ? (

What does defined mean in C++?

The #define creates a macro, which is the association of an identifier or parameterized identifier with a token string. After the macro is defined, the compiler can substitute the token string for each occurrence of the identifier in the source file.

What is inline CPP?

Inline function in C++ is an enhancement feature that improves the execution time and speed of the program. The main advantage of inline functions is that you can use them with C++ classes as well.


1 Answers

The truth is in the standard (3.2 One definition rule) :

No translation unit shall contain more than one definition of any variable, function, class type, enumeration type or template.

[...]

Every program shall contain exactly one definition of every non-inline function or object that is used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is used.

like image 67
icecrime Avatar answered Sep 23 '22 23:09

icecrime