Topic: Proposal: in-class initialization of reference type static class members 9.4.2


Author: yecril@bluebottle.com ("Krzysztof elechowski")
Date: Mon, 16 May 2005 16:20:18 GMT
Raw View
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                       ]