Date: Sun, 20 Oct 2024 20:02:32 +0200
On 20/10/2024 19.02, Federico Kircheis via Std-Discussion wrote:
> The two TU have a global variable named instance of the same type at
> different addresses.
>
> Having
>
> ----
>> file1.hpp
> const int i = 1;
>
>> file2.cpp
> #include "file1.hpp"
>
>> file3.cpp
> #include "file1.hpp"
> ----
>
> and
>
> ----
>> file2.cpp
> const int i = 1;
>
>> file3.cpp
> const int i = 1;
> ----
>
> is equivalent, and to the best of my knowledge not problematic.
> The two variables are declared independently, at two different locations.
>
> The standard says
>
>> No translation unit shall contain more than one definition of any definable item [...]
This is the rule that governs multiple definitions within the same translation
unit.
> If something else claims that this is an ODR, a pointer to the relevant
> content is greatly appreciated.
[basic.def.odr] p15 covers definitions across different translation units.
Note that we're talking about a variable, so p15.1 applies.
However, before we get there, we have to check whether the two things named
"i" here refer to the same entity, which is covered by [basic.link].
In particular, p3.2 says that "i" has internal linkage because it has
const-qualified type. So, for this case, we're actually talking about two
different variables that are not related. If you leave out the "const"s,
you get an ODR-violation.
Jens
> The two TU have a global variable named instance of the same type at
> different addresses.
>
> Having
>
> ----
>> file1.hpp
> const int i = 1;
>
>> file2.cpp
> #include "file1.hpp"
>
>> file3.cpp
> #include "file1.hpp"
> ----
>
> and
>
> ----
>> file2.cpp
> const int i = 1;
>
>> file3.cpp
> const int i = 1;
> ----
>
> is equivalent, and to the best of my knowledge not problematic.
> The two variables are declared independently, at two different locations.
>
> The standard says
>
>> No translation unit shall contain more than one definition of any definable item [...]
This is the rule that governs multiple definitions within the same translation
unit.
> If something else claims that this is an ODR, a pointer to the relevant
> content is greatly appreciated.
[basic.def.odr] p15 covers definitions across different translation units.
Note that we're talking about a variable, so p15.1 applies.
However, before we get there, we have to check whether the two things named
"i" here refer to the same entity, which is covered by [basic.link].
In particular, p3.2 says that "i" has internal linkage because it has
const-qualified type. So, for this case, we're actually talking about two
different variables that are not related. If you leave out the "const"s,
you get an ODR-violation.
Jens
Received on 2024-10-20 18:02:36