[cfe-dev] Clang and C++Ox : what I'm doing wrong
John McCall
rjmccall at apple.com
Fri Oct 8 09:30:53 PDT 2010
On Oct 8, 2010, at 9:14 AM, James Noble wrote:
> On Oct 8, 2010, at 11:47 AM, Argyrios Kyrtzidis wrote:
>> Why are we requiring a definition for Test::value, isn't this supposed to be used as a constant ? John ?
>
> I think the C++ standard mandates that even a static const variable must have a
> definition. I think some compilers allow integral types to skirt around that but
> I think this is why there is the "enum hack" for C++ ...
This is not quite correct. The standard says that a definition must given for a static const literal-typed data member if it is used in the program. "Used" is actually a term of art from [basic.def.odr]p2:
A variable or non-overloaded function whose name appears as a potentially-evaluated expression is used unless it is an object that satisfies the requirements for appearing in a constant expression and the lvalue-to-rvalue conversion is immediately applied.
So this is actually a point of non-compliance for us with C++0x, which is okay because we don't actually claim to support C++0x. Nonetheless, we should implement this.
The enum hack is useful mostly because it's easy to accidentally bind a reference to a const static data member and thus "use" it according to the standard — usually because some template function takes a 'const T &' (to avoid unnecessary copies of aggregate arguments) and then 'T' gets instantiated with 'int' and suddenly you're binding to the variable instead of a temporary. Never let it be said that C++ is not an awful language.
John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20101008/ee854046/attachment.html>
More information about the cfe-dev
mailing list