C++ Logo

sg10

Advanced search

Re: [SG10] How closely should our recommendation match clang?

From: John Spicer <jhs_at_[hidden]>
Date: Fri, 25 Jan 2013 10:29:10 -0500
On Jan 24, 2013, at 7:40 PM, Nelson, Clark wrote:

> We can't ignore clang, as it is the only popular implementation that has done anything systematic about language feature-testing. But what clang has already done about C++11 (i.e. the past) isn't directly applicable to the future; it's only a model. On the other hand, it's very instructive, especially as regards granularity.
>
> To me, the most obvious question is whether testing for the presence of a feature should use a function-like syntax, or just an identifier. From a programming perspective, there's no practical difference between these two alternatives:
>
> __has_feature(cxx_alignas)
> __has_feature_cxx_alignas
>
> On the other hand, if the function-like syntax is used to test for a feature, somehow there has to be a definition of the function-like macro, or simply testing for the feature is likely to be an error. That would either limit the usefulness of feature-testing, or require users to add boilerplate to conditionally define the function-like macro.
>

Whichever form is used, either the compiler or a header is going to need to define the necessary identifiers in order for programs that use them to compile correctly with the desired features.

I have a mild preference for the function-like syntax. If an implementation chooses to define the other style of macros, it is easy to provide a mapping from the function-style to the as a predefined macro, a macro in a header file, or a command-line macro definition.

#define __has_feature(x) __has_feature_##x
#define __has_feature_test 1

#if __has_feature(test)
static_assert(false, "has test");
#else
static_assert(false, "does not have test");
#endif

If the definition of __has_feature(x) is removed, it can be put on the command-line, as in:

        CC -"-D__has_feature(x)=__has_feature_##x" t1.c

Because most existing compilers with C++11 support do not support __has_feature, the function-style is in some ways an advantage in that if you compile code that uses such a feature, you will get an error instead of potentially getting the wrong version of a given piece of code (if the compiler has alignas but not __has_feature).

John.

Received on 2013-01-25 16:29:16