[cfe-commits] [cfe-dev] [patch] add cxx_auto and cxx_for_range to has_extension
Richard Smith
richard at metafoo.co.uk
Mon Sep 5 06:02:26 PDT 2011
Hi,
On Mon, September 5, 2011 09:20, Chandler Carruth wrote:
> On Mon, Sep 5, 2011 at 12:46 AM, Jean-Daniel Dupas <jddupas at gmail.com>wrote:
>> I just saw that a recent commit add support of "auto type" and "for range"
>> to c++98 as language extension. (r39102: PR10458: Finesse behaviour of C++0x
>> features when in pre-0x mode. Accept for-range and auto with an ExtWarn,
>> and produce a -Wc++0x-compat warning in C++98 mode when auto is used as a
>> storage class.)
>
>> Shouldn't be this changed reflected in the __has_extension macro like other
>> language extensions ?
>>
>> I attach a patch to fix this inconsistency.
>
> Seems likely good, but I'll let the person who flipped the switch chime in.
The for-range part seems fine.
The situation with 'auto' is more nebulous. C++0x introduces three changes
with respect to the 'auto' keyword:
a) it's no longer a storage class specifier,
b) it's now a type specifier, and
c) it's now used in function declarations with late-specified return types.
__has_feature(cxx_auto_type) currently covers (a) and (b), but the extension
covers only (b): we treat 'auto' as a type-specifier in C++98 only if there is
no other type specifier.
Sadly, this distinction matters in practice, for instance in the following
examples (taken from N2337):
typedef int T;
int x1, x2, y;
auto T(&x1) = y; // (1)
auto T(&x2); // (2)
auto T = 3; // (3)
auto T(y); // (4)
In C++0x, 'auto' is always a type specifier, so (1) is an error, (2) declares
an int* called T, (3) declares an int called T and (4) declares an int called
T.
In C++98 (with or without the extension), 'auto' is treated as a storage class
specifier in these cases, because 'T' is treated as a type specifier (and the
longest sequence of such specifiers which could denote a type is always used).
So (1) declares a reference called x1, (2) is an error (reference must be
initialized), (3) is an error (missing variable name) and (4) declares an int
called y.
If we want to enable __has_extension(cxx_auto_type), we should also update the
documentation to indicate precisely what it means, and we might benefit from a
separate __has_feature() flag for (a). My current preference would be to
promote the warning to an error (and use the "extension" for error recovery
only).
Richard
More information about the cfe-commits
mailing list