C++ Logo


Advanced search

Re: [SG10] Example for __cpp_lib_byte

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Sun, 22 Oct 2017 18:41:31 +0300
On 22 October 2017 at 13:25, Bjarne Stroustrup <bjarne_at_[hidden]> wrote:
>>> I fear the utility of feature macros to define dialects: "if (feature I
>>> don't like) static_assert(false, "don't use this; it's bad for you)."
>>> Fortunately this is (for me) mainly hypothetical, but over the years I
>>> have had *many* hopeful questions along the lines of "is there a
>>> standard way of disabling X?". I strongly prefer to leave such potential
>>> major damage for local gain to non-standard hacks.
>> How can a feature macro be used to disable a feature???
> Simple. As I said above:
> static_assert(false, "don't use this; it's bad for you");
> Place this in a header and require it for corporate use and you are done.
> I hope I'm not giving you new bad ideas. As I said, the equivalent has been frequently suggested to me over the years.

So, for a library feature, this might be done:

#ifdef __cpp_optional
static_assert(false, "optional is bad, don't use it");

For language features, such subsetting doesn't work at all. The
compiler defines feature macros regardless
of whether you actually use the feature, so your program would be
ill-formed just because it's compiled
with a compiler that supports said feature. And even the
library-subsetting attempt will not detect
whether you actually use e.g. optional, it will just prevent including
the header that defines the feature macro.
And you need to do the feature check after you include optional, not before.

Using feature macros for subsetting that way doesn't seem simple, nor
does it seem realistic.

Received on 2017-10-22 17:41:34