Questions tagged [one-definition-rule]

Anything related to C++ One Definition Rule (ODR), i.e. a rule of the C++ standard banning multiple definitions of most language entities. The ODR roughly mandates that most language entities (objects, functions, templates, etc.) must have a unique (non-duplicated) definition in the same translation unit or across the entire program, while multiple declarations are still possible.

The One Definition Rule (ODR) is the concept that there is at most one defined instance of a function or object allowed in a program.

In C, the One Definition Rule is described in C11 Section 6.9 in paragraph 3:

There shall be no more than one external definition for each identifier declared with internal linkage in a translation unit. Moreover, if an identifier declared with internal linkage is used in an expression (other than as a part of the operand of a sizeof or _Alignof operator whose result is an integer constant), there shall be exactly one external definition for the identifier in the translation unit.

In C++, the One Definition Rule is explained in C++11 Section 3.2 [basic.def.odr], but succinctly summarized in the first paragraph:

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

Inlined functions have their own exception clauses. Extensions to the C language relax the one definition rule strictness by permitting multiple definitions if their declarations are all compatible.

311 questions
738
votes
16 answers

When should I write the keyword 'inline' for a function/method?

When should I write the keyword inline for a function/method in C++? After seeing some answers, some related questions: When should I not write the keyword 'inline' for a function/method in C++? When will the compiler not know when to make a…
Partial
  • 9,529
  • 12
  • 42
  • 57
112
votes
2 answers

What does it mean to "ODR-use" something?

This just came up in the context of another question. Apparently member functions in class templates are only instantiated if they are ODR-used. Could somebody explain what exactly that means. The wikipedia article on One Definition Rule (ODR)…
Sarien
  • 6,647
  • 6
  • 35
  • 55
70
votes
2 answers

Can using a lambda in header files violate the ODR?

Can the following be written in a header file: inline void f () { std::function func = [] {}; } or class C { std::function func = [] {}; C () {} }; I guess in each source file, the lambda's type may be different and therefore the…
Alex Telishev
  • 2,264
  • 13
  • 15
59
votes
4 answers

Do distinct functions have distinct addresses?

Consider these two functions: void foo() {} void bar() {} is it guaranteed that &foo != &bar? Similarly, template void foo() { } is it guaranteed that &foo != &foo? There are two linkers I know of that fold function…
45
votes
2 answers

Why are const ints (or shorts) captured implicitly in lambdas?

This compiles: int main() { const int x = 123; auto g = []() { std::cout << x << "\n"; }; g(); } But this: int main(){ const float x = 123; auto g = []() { std::cout << x << "\n"; }; g(); } produces: "error: 'x' is not…
rubix_addict
  • 1,811
  • 13
  • 27
29
votes
4 answers

c & c++ default global variable linkage, multiple declaration & definition problem

For example: code1.c / .cpp int a; // ... and so on code2.c / .cpp int a; int main(void) { return 0; } go to compile: $gcc code1.c code2.c # this is fine $ $g++ code1.cpp code2.cpp # this is dead /tmp/ccLY66HQ.o:(.bss+0x0): multiple…
Bossliaw
  • 698
  • 1
  • 10
  • 23
28
votes
6 answers

Why is multiple definition of a const global variable allowed in C++ and not in C?

Multiple definition of a global variable is not allowed in C or C++ due to the One Definition Rule. However, in C++ a const global variable can be defined in multiple compilation units with no error. This is not the same as in C. Why does C++ allow…
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
26
votes
1 answer

When is a variable odr-used in C++14?

The C++14 draft (N3936) states in §3.2/3: A variable x whose name appears as a potentially-evaluated expression ex is odr-used unless applying the lvalue-to-rvalue conversion (4.1) to x yields a constant expression (5.19) that does not invoke any…
MWid
  • 4,429
  • 3
  • 20
  • 20
25
votes
1 answer

Should `const` and `constexpr` variables in headers be `inline` to prevent ODR violations?

Consider the following header and assume it is used in several TUs: static int x = 0; struct A { A() { ++x; printf("%d\n", x); } }; As this question explains, this is an ODR violation and, therefore, UB. Now, there is no…
Acorn
  • 24,970
  • 5
  • 40
  • 69
24
votes
2 answers

Doesn't std::piecewise_construct cause a ODR violation?

std::piecewise_construct, defined in , has internal linkage since it is declared constexpr. I wonder if use of std::piecewise_construct in a header can violate ODR. For example: a.hpp #include #include struct point { …
iorate
  • 693
  • 5
  • 6
20
votes
4 answers

If inlining is optional, why does removing 'inline' cause linker errors?

I have a class that had an inline member, but I later decided that I wanted to remove the implementation from the headers so I moved the members body of the functions out to a cpp file. At first I just left the inlined signature in the header file…
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
20
votes
1 answer

ODR bug in MSVC?

This program prints 1 1 instead of 1 2 when compiled with MSVC (up to VS 2015). f1.cpp: #include static std::function helper() { struct F { int operator()() { return 1; } }; return F(); } std::function f1() {…
Valentin Milea
  • 3,186
  • 3
  • 28
  • 29
20
votes
2 answers

Is there a way to detect inline function ODR violations?

So I have this code in 2 separate translation units: // a.cpp #include inline int func() { return 5; } int proxy(); int main() { printf("%d", func() + proxy()); } // b.cpp inline int func() { return 6; } int proxy() { return func();…
onqtam
  • 4,356
  • 2
  • 28
  • 50
19
votes
2 answers

Necessity of forward-declaring template functions

I recently created this example code to illustrate C++11 variadic template function usage. template void foo (Head, Tail...); template void foo (int, Tail...); void foo () {} template…
spraff
  • 32,570
  • 22
  • 121
  • 229
19
votes
3 answers

Static variable in a Header File

Static variable has file scope. Say I have two following files: file1.h file1.cpp file2.h file2.cpp I have declared static variable say static int Var1 in both the header files. Both file1.h and file2.h are included in main.cpp file. I did…
Vikram Ranabhatt
  • 7,268
  • 15
  • 70
  • 133
1
2 3
20 21