Re: static const integral members
  Home FAQ Contact Sign in
comp.lang.c++.moderated only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: static const integral members         

Group: comp.lang.c++.moderated · Group Profile
Author: Daniel Krügler
Date: Apr 13, 2008 16:30

On 13 Apr., 20:45, Carl Barron adelphia.net> wrote:
> The point is
> struct foo
> {
> static const int value = 1;
> };
>
> does not automatically violate ODR only different defintions of foo
> would do so. The sniplet does not involve multiple defintions of foo
> or
> foo::value. and the sniplet is legal and valid.

This is an incorrect/misleading statement as written. The
ODR is does not only rule *multiple* definitions, it does
also rule *missing* definitions. You can interpret that as
a *one-and-only-one-definition*. [basic.def.odr]/3 says
precisely this:

"Every program shall contain exactly one definition of
every non-inline function or object that is used in that
program; no diagnostic required. The definition can appear
explicitly in the program, it can be found in the standard
or a user-defined library, or (when appropriate) it is
implicitly defined (see 12.1, 12.4 and 12.8). An inline
function shall be defined in every translation unit in
which it is used."

For exactly this reason the whole (and quite long)
paragraph 2 defines the term "use". And the relevant
point which the OP points out is that the use-case

struct foo
{
static const int value = 1;
};

int main()
{
int bar = foo::value;
}

belongs to the category "use" in the current standard.
So there is a *use* *without* any definition and this is
a violation of the ODR and brings us into UB.

Neither the OP nor I did say, that every such example
violates the ODR. E.g. the example you provide in your
most recent answer does *not* violate the ODR as currently
visible (but it would require a complete program like

struct foo
{
static const int value = 1;
};

int main()
{
}

to discuss in a meaningful way about that point) and
the OP himself showed a use-case which does not require
a definition even according to the recent standard because
there is no "use". This was the example where the value of
foo::value appears as initialization value of an array-bound.
This was the reason of his quote of [basic.def.odr]/2:

"[..] An expression is potentially evaluated unless it
appears where an integral constant expression is required[..]"

HTH & Greetings from Bremen,

Daniel Krügler

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
no comments
diggit! del.icio.us! reddit!