Maybe its lame question, But I don't get it!
If I include <string> or <vector> in multiple translation units (different .cpp) why it doesn't break the ODR?
As far as I know each .cpp is compiled differently so vector's methods code will be generated for each object file separately, right?
So linker should detect it and complain.
Even If it won't (I suspect it's special case for templates) will it be using one code or different set of cloned code in each unit, when I link all together???
- 14,837
- 4
- 49
- 83
- 1,964
- 1
- 13
- 25
-
3Essentially, the compiler and linker conspire to make it work, using the same mechanism that inline functions use. – Igor Tandetnik Dec 31 '15 at 23:26
-
1As you suspect it is a special case for templates, like for inline functions. Definition in different files should be exactly right to not violate ODR. – Revolver_Ocelot Dec 31 '15 at 23:28
-
3barney: Why don't you try picking out a specific phrase in the ODR that you think is violated, and why the stated exceptions don't apply? – Chris Beck Dec 31 '15 at 23:28
3 Answers
The same way any template definitions don't break the ODR — the ODR specifically says that template definitions may be duplicated across translation units, as long as they are literally duplicates (and, since they are duplicates, no conflict or ambiguity is possible).
[C++14: 3.2/6]:There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements [..]
Multiple inclusions of <vector> within the same translation unit are expressly permitted and effectively elided, more than likely by "#ifndef" header guards.
- 369,052
- 73
- 620
- 1,021
-
I see... But what about code duplication?? vector class contains code (well, templatized and abstract enough but still code...). So it would generate code for each inclusion in each translation unit where I use it, right? So, even if I use std::vector
everywhere, very identical code should be generated separately for each module... that looks unoptimal... – barney Dec 31 '15 at 23:34 -
1@barney: Yes, it is sub-optimal. C++'s compilation model (mostly inherited from C) has its flaws, and the addition of templates made them worse. That's one of the big reasons that C++ compilation is seen as being so remarkably slow — it has to parse each definition for _every_ compilation unit. And then it has to resolve all those duplicates and elide them at link time. Nobody's saying that this is the best way it can be done, only that it is the way C++ does it. :) – Lightness Races in Orbit Dec 31 '15 at 23:35
-
-
1The compiler will in fact generate a `vector
` implementation in every compilation unit where it's needed. Sometimes that means inline code (most methods in `vector` are quite lightweight) and other times it means standalone functions. The linker will sort it out and elide any duplicate copies of standalone functions. – StilesCrisis Dec 31 '15 at 23:36 -
@LightnessRacesinOrbit They keep trying to come up with a workable "modules" implementation...no luck yet. – Zan Lynx Dec 31 '15 at 23:36
-
@barney: the linker is supposed to eliminate those duplicates, so that if you take an address `&f` where `f` is a class member function of some template like `vector
` or something, it should give the same address no matter what compilation unit you are in – Chris Beck Dec 31 '15 at 23:36 -
I see... so the compiler makes (theoretically) redundant code and linker removes all the duplicates, right? Yes, translation modules would be awesome feature to resolve it. Anyway I love c++ for its zero cost abstractions approach! ) – barney Dec 31 '15 at 23:39
-
@LightnessRacesinOrbit btw if templates are not classes but "instructions for compiler to generate classes's code" why not to add full fledged meta-code there? current template syntax is weird and cryptic (especially overloaded/templated/specialized versions matching and type traits tricks). Woudn't it be nice to have syntax for compilation time runable c++ code that will explicitly define all template generation rules in source code. Maybe crazy idea :) maybe not C++ but special DSL language for code generation. :) – barney Jan 01 '16 at 00:01
-
3@barney: Well then that would be a different language wouldn't it – Lightness Races in Orbit Jan 01 '16 at 00:13
-
Formally, the last sentence is not 100% correct. Multiple inclusion of the same standard header in the same translation unit is covered by § 17.6.2.2/2, which says: *"Each may be included more than once, with no effect different from being included exactly once (...)"*. Header guards are but an *implementation* of this rule :) – Christian Hackl Jan 01 '16 at 13:39
-
The standard has a special exception for templates that allows for duplication of functions that otherwise would violate ODR (such as functions with external linkage and non-inline member functions). from C++11 3.2/5:
If D is a template and is defined in more than one translation unit, then the preceding requirements shall apply both to names from the template’s enclosing scope used in the template definition (14.6.3), and also to dependent names at the point of instantiation (14.6.2). If the definitions of D satisfy all these requirements, then the program shall behave as if there were a single definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined.
- 321,763
- 49
- 514
- 739
The ODR doesn't state that a struct will only be declared one time across all compilation units--it states that if you declare a struct in multiple compilation units, it has to be the same struct. Violating the ODR would be if you had two separate vector types with the same name but different contents. At that point the linker would get confused and you'd get mixed up code and/or errors.
- 15,646
- 4
- 35
- 58
-
1And this is exactly what happens when you modify a definition in a header but don't rebuild all translation units including that header. – Lightness Races in Orbit Sep 07 '18 at 09:39