Topic: Static const data member initializers


Author: bill@gibbons.org (Bill Gibbons)
Date: 1996/03/18
Raw View
In article <0099F84782C14100.3D6072A5@ittpub.nl>, "Wil Evers"
<wil@ittpub.nl> wrote:

> Does anyone know the rationale behind requiring a definition of a static
> const data member that has been declared with an initializer? I mean, if I
> write:
>
>         // header file
>
>         class X { static const int bufSize = 42; };
>
> then why should I be required to write
>
>         // some translation unit
>
>         const int X::bufSize;
>
> It seems to me that interpreting bufSize's declaration-with-initializer in
> the class definition as a definition in class scope with internal linkage
> (analogous to an inline member function) would make more sense.

But inline member functions have *external* linkage.  To make the ODR
(One Definition Rule) as useful as possible, it is necessary to enforce it
on all class members.

So the "make a copy in each file as needed" approach commonly used for
inline non-member functions will not work here - it would break the ODR.
And while there are ways to ensure that only one copy is actually used,
it was felt that requiring somewhat complex implementation tricks for
such a simple feature was inappropriate.

--
Bill Gibbons
bill@gibbons.org
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Wil Evers" <wil@ittpub.nl>
Date: 1996/03/18
Raw View
The DWP, section 9.5.2. [class.static.data], clause 4, says:

`If a static data member is of const integral or const enumeration type,
its declaration in the class definition can specify a constant-initializer
which shall be an integral constant expression (5.19). In that case, the
member can appear in integral constant expressions within its scope. The
member shall still be defined in a namespace scope and definition of the
member in namespace scope shall not contain an initializer.'

Does anyone know the rationale behind requiring a definition of a static
const data member that has been declared with an initializer? I mean, if I
write:

        // header file

        class X { static const int bufSize = 42; };

then why should I be required to write

        // some translation unit

        const int X::bufSize;

It seems to me that interpreting bufSize's declaration-with-initializer in
the class definition as a definition in class scope with internal linkage
(analogous to an inline member function) would make more sense.

Thanks in advance,

- Wil

Wil Evers, <wil@ittpub.nl>
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]