Thread View: comp.std.c++
2 messages
2 total messages
Started by Wil Evers
Mon, 18 Mar 1996 16:41
Static const data member initializers
Author: Wil Evers
Date: Mon, 18 Mar 1996 16:41
Date: Mon, 18 Mar 1996 16:41
38 lines
1996 bytes
1996 bytes
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 ]
Re: Static const data member initializers
Author: Bill Gibbons
Date: Mon, 18 Mar 1996 17:51
Date: Mon, 18 Mar 1996 17:51
41 lines
2249 bytes
2249 bytes
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
]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads