Topic: Defect report: variables with linkage defined in block scope
Author: "Richard Smith" <richard@metafoo.co.uk>
Date: Sat, 12 Nov 2011 11:33:49 -0800 (PST)
Raw View
Hi,
C89 6.5.7 and C99 6.7.8/5 say "If the declaration of an identifier has block
scope, and the identifier has external or internal linkage, the declaration
shall have no initializer for the identifier."
C++ appears to have no such restriction (though if someone can point out where
such a restriction lives, I'll happily stand corrected). Hence cases such as
the following appear to be legal:
template<int v> void f() { extern const int k = v; }
... and ...
extern const int n;
struct S {
S() {
extern const int n = 5;
}
int array[n]; // ok, n is an ICE!
};
A careful reading of both C++98 and C++11 indicate that, since such an entity
has static storage duration, if it is not eligible for constant initialization
then it is initialized the first time its definition is evaluated (when the
containing function is first called).
I have tested EDG, g++ and clang, and all of them implement the C restriction
even in C++. This seems like the right choice, so should be specified by the
standard!
Thanks,
Richard
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?=<daniel.kruegler@googlemail.com>
Date: Sun, 13 Nov 2011 15:26:15 -0800 (PST)
Raw View
Am 12.11.2011 20:33, schrieb Richard Smith:
> C89 6.5.7 and C99 6.7.8/5 say "If the declaration of an identifier has block
> scope, and the identifier has external or internal linkage, the declaration
> shall have no initializer for the identifier."
Just for the record: The recent C11 draft has above quoted wording in
6.7.9/5.
> C++ appears to have no such restriction (though if someone can point out where
> such a restriction lives, I'll happily stand corrected).
I haven't found corresponding words either in C++, I therefore forwarded
your report to CWG.
Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]