 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Krzysztof Żelechowski Guest
|
Posted: Mon May 16, 2005 4:20 pm Post subject: Proposal: in-class initialization of reference type static c |
|
|
II Motivation
I ran into this problem when I tried to create an alias for global data that
methods of my class use on a regular basis. However, it is impossible to
treat such a reference as an alias because the target object is not known at
compile time.
III Impact on the Standard
[class.static.data] should be changed to allow in-class initialization of
reference type static class members. It should not be considered a language
extension because the restriction of in-class initialization to integer
constants is too restrictive.
IV Design Decisions
There is a workaround to define a static inline method to return a reference
to global data. However, it is clumsy because the method has to be called
so there is no quick transition from using a static member to using a static
reference.
This extension will not affect compilation of existing code base. The
implementation is to replace the member reference where it is used with a
reference to the global data it refers to.
There is a way to do it with global references: you can define a static
reference to global data in a header file and use it seamlessly in the code.
Example:
#ifdef UNICODE
static basic_stream<wchar_t> &tcout = wcout;
#else
static basic_stream<char> &tcout = cout;
#endif
I would like to port this construction to static data members.
V. Proposed Text for the Standard
9.4.2
9 If a static data member is of reference type, its declaration in the class
definition can specify an initializer of the same type which shall be a
static object referenced by an identifier or an accessible member thereof or
an item of a static array referenced by an identifier indexed with an
integer constant.
The member shall still be defined in a namespace
scope if it is used in the program and the namespace scope definition shall
not contain an initializer.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|