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?
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."
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 ? (
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With