Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Daniel Krügler<daniel.kruegler@googlemail.com> |
|---|---|
| Newsgroups | comp.std.c++ |
| Subject | Re: constants vs. ODR |
| Date | 2011-04-28 15:46 -0600 |
| Organization | A noiseless patient Spider |
| Message-ID | <ipcd9c$3tu$1@dont-email.me> (permalink) |
| References | <ipa3hd$1si1$1@news.ett.com.ua> |
Am 28.04.2011 20:19, schrieb Balog Pal:
>
> If I have the following in a header:
>
> const int NUM = 42;
> inline void foo(const int&) { ... } // could be template, etc
> inline void bar()
> {
> foo(NUM);
> }
>
> If I understand correctly, by the current standard this will break the one
> definition rule, if the header is included in multiple translation units.
> Because of the const hack that makes it static, so NUM will really be
> different in every TU, therefore bar() will have non-identical definitions.
> Placing the program in UB land.
I don't see any reason for a concrete violation of the ODR in this
example as *written*. Relevant quotes are from the FDIS (the
corresponding definitions exist for C++03 already and are provided in
parentheses additionally below).
NUM is a variable with internal linkage and it is defined once per
translation unit, so there will be exactly one NUM per translation unit
as it should be.
1) [dcl.stc] p. 7 (p. 6 in C++ 2003):
"A name declared in a namespace scope without a storage-class-specifier
has external linkage unless it has internal linkage because of a
previous declaration and provided it is not declared const. Objects
declared const and not explicitly declared extern have internal linkage."
2) [basic.link] p. 3 (same location in C++ 2003):
"A name having namespace scope (3.3.6) has internal linkage if it is the
name of
[..]
— a variable that is explicitly declared const or constexpr and neither
explicitly declared extern nor previously declared to have external linkage;
[..]"
Both bar and foo are inline function (with external linkage).
See (1)+(2) and see
(3) [basic.def.odr] p. 5 (same location in C++ 2003, but restricted to
integral types and enums at the point where literal types are mentioned):
"There can be more than one definition of a [..] inline function with
external linkage (7.1.2), [..] in a program provided that each
definition appears in a different translation unit, and provided the
definitions satisfy the following requirements. Given such an entity
named D defined in more than one translation unit, then
[..]
— in each definition of D, corresponding names, looked up according to
3.4, shall refer to an entity defined within the definition of D, or
shall refer to the same entity, after overload resolution (13.3) and
after matching of partial template specialization (14.8.3), except that
a name can refer to a const object with internal or no linkage if the
object has the same literal type in all definitions of D, and the object
is initialized with a constant expression (5.19), and the value (but not
the address) of the object is used, and the object has the same value in
all definitions of D;"
You are not showing us any content of the body of foo(), so we don't
know whether foo() might violate the ODR when referring to it's function
argument. If foo would be written like this:
#include<iostream>
const int NUM = 42;
inline void foo(const int& arg) { std::cout<< &arg<< std::endl; }
inline void bar() { foo(NUM); }
it would violate the ODR, yes. The same could happen for an analogously
written function bar_2() as follows:
#include<iostream>
const int NUM_2 = 42;
inline void bar_2() { std::cout<< &NUM_2<< std::endl; }
> My question is whether this was fixed for C++0x?
What do you want to have fixed?
HTH& Greetings from Bremen,
Daniel Krügler
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to comp.std.c++ | Previous | Next — Previous in thread | Next in thread | Find similar
constants vs. ODR "Balog Pal" <pasa@lib.hu> - 2011-04-28 12:19 -0600
Re: constants vs. ODR Johannes Schaub<schaub.johannes@googlemail.com> - 2011-04-28 15:47 -0600
Re: constants vs. ODR Daniel Krügler<daniel.kruegler@googlemail.com> - 2011-04-28 15:46 -0600
Re: constants vs. ODR "Balog Pal"<pasa@lib.hu> - 2011-05-01 10:14 -0600
Re: constants vs. ODR Johannes Schaub <schaub.johannes@googlemail.com> - 2011-05-01 12:52 -0600
Re: constants vs. ODR Daniel Krügler <daniel.kruegler@googlemail.com> - 2011-05-06 13:07 -0600
csiph-web