Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #136

Re: constants vs. ODR

From Johannes Schaub<schaub.johannes@googlemail.com>
Newsgroups comp.std.c++
Subject Re: constants vs. ODR
Followup-To comp.std.c++
Date 2011-04-28 15:47 -0600
Organization Arcor
Message-ID <4db9a93e$0$7665$9b4e6d93@newsspool1.arcor-online.net> (permalink)
References <ipa3hd$1si1$1@news.ett.com.ua>

Followups directed to: comp.std.c++

Show all headers | View raw


Balog Pal wrote:

>
>  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.
>
>  My question is whether this was fixed for C++0x?
>

This wasn't really fixed, in my opinion. The ODR in the spec says that NUM
should be used in the way that "the value (but not the address) of the
object is used", in order to make the above example well-defined when
included into multiple TUs.

In your case, you bind the object to a reference, and I would say that this
crosses the boundary of allowed interpretation variety of that ODR rule.


-- 
[ 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 | NextPrevious in thread | Next in thread | Find similar


Thread

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