220 31773 <afbe68ce-4606-06f0-e270-3dc4ff911bc4@scylladb.com> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Avi Kivity <avi@scylladb.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Allow constexpr static data members in a literal type.
Date: Mon, 27 Mar 2017 11:13:48 +0300
Organization: ScyllaDB
Lines: 109
Approved: news@gmane.org
Message-ID: <afbe68ce-4606-06f0-e270-3dc4ff911bc4@scylladb.com>
References: <dfb9f65c-ea1d-e92b-85e0-333efed1aec5@scylladb.com>
 <5262b75e-8281-411d-2248-6be37fd52b85@wanadoo.fr>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable
X-Trace: blaine.gmane.org 1490602439 3695 195.159.176.226 (27 Mar 2017 08:13:59 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Mon, 27 Mar 2017 08:13:59 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
 Thunderbird/45.8.0
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDHJFOWD7YCRBP4T4PDAKGQEOQTDZYY@isocpp.org Mon Mar 27 10:13:54 2017
Return-path: <std-proposals+bncBDHJFOWD7YCRBP4T4PDAKGQEOQTDZYY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-wr0-f200.google.com ([209.85.128.200])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDHJFOWD7YCRBP4T4PDAKGQEOQTDZYY@isocpp.org>)
	id 1csPmc-00086L-7p
	for gclcip-std-proposals@m.gmane.org; Mon, 27 Mar 2017 10:13:46 +0200
Original-Received: by mail-wr0-f200.google.com with SMTP id i18sf11804901wrb.21
        for <gclcip-std-proposals@m.gmane.org>; Mon, 27 Mar 2017 01:13:52 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=subject:to:references:from:organization:message-id:date:user-agent
         :mime-version:in-reply-to:content-transfer-encoding
         :x-original-sender:x-original-authentication-results:reply-to
         :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post
         :list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=LJWByAHCPWk4U8OvLePNjjub+o7QmMxB4zTfWutAXeY=;
        b=rjWMayKYXRujXxPxMubh4Lf3mL8TKP0WQGBLTvftG0X4XdqU+4co78gLNw18my2nfw
         JAZoDTBHOfcOLRruMLq/uPvc/cCMIobBp73TXdl9bpJ7lU1WsevJTYYc2KikOjGwojhR
         JiCzMO2PI93bqHHolU7XlnsyeY+cVNihqhSbc7lMiLmiXFI3HKmgQ3PrQWZHTNmqE94u
         R/OeqybAhmRA5yS47o/amk+S0pmyWgb0X2ZiXzZqKRilOHsI0EicsZWNO+sCNeiEcpu8
         Q2htCbcdhW5EhVSmgdoZEbzpNLH1s5PB/pZnK0PHN15GJskH/CX1SMiqqIJeP 
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:subject:to:references:from:organization
         :message-id:date:user-agent:mime-version:in-reply-to
         :content-transfer-encoding:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=LJWByAHCPWk4U8OvLePNjjub+o7QmMxB4zTfWutAXeY=;
        b=B5ZaYrqc0tFrLqHr4YEyaZjtc/xJyyXfgrRPXZVdP1q7cceee+rtf9P+fx4WnAE/3n
         9x5O4a/Ef/wh9mws7ApDg38CJ6gjtu71f4YByGc+G6u8bflAz2neMtrG4MlFEqaZ6VVp
         14yNR4idGAzHIgenvuG8P9n3hcoAVrkgMxpOL3LT7jeB+y93ZFjC9JTPmjIDGzc8KFx9
         2J4BSq8J/9o8QJPi2kwjaxwck9t20Zh+N118SFxTNrEG/gz05eqqpW4uNrw5wZrfJxKp
         AvwpuG6VgOTHP1nQPF5cyD5zd69IAgNRV7JKptNBpT38Fpq 
X-Gm-Message-State: AFeK/H2zcDINzv9nVXb6qXWpNr4mXIWdGx6StV8rYtb2ShEb1PSXS5TnAz+UeJtPVWIc3Q==
X-Received: by 10.28.157.66 with SMTP id g63mr395628wme.22.1490602432340;
        Mon, 27 Mar 2017 01:13:52 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.45.211 with SMTP id t202ls317374wmt.17.canary-gmail; Mon,
 27 Mar 2017 01:13:51 -0700 (PDT)
X-Received: by 10.28.109.147 with SMTP id b19mr8530456wmi.69.1490602431299;
        Mon, 27 Mar 2017 01:13:51 -0700 (PDT)
Original-Received: from mail-wr0-x22c.google.com (mail-wr0-x22c.google.com. [2a00:1450:400c:c0c::22c])
        by mx.google.com with ESMTPS id b4si13254738wmb.98.2017.03.27.01.13.51
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Mon, 27 Mar 2017 01:13:51 -0700 (PDT)
Received-SPF: pass (google.com: domain of avi@scylladb.com designates 2a00:1450:400c:c0c::22c as permitted sender) client-ip=2a00:1450:400c:c0c::22c;
Original-Received: by mail-wr0-x22c.google.com with SMTP id l43so43934677wre.1
        for <std-proposals@isocpp.org>; Mon, 27 Mar 2017 01:13:51 -0700 (PDT)
X-Received: by 10.28.225.69 with SMTP id y66mr3240160wmg.84.1490602430944;
        Mon, 27 Mar 2017 01:13:50 -0700 (PDT)
Original-Received: from avi.cloudius-systems.com ([37.142.229.250])
        by smtp.gmail.com with ESMTPSA id m186sm13318379wmd.21.2017.03.27.01.13.50
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Mon, 27 Mar 2017 01:13:50 -0700 (PDT)
In-Reply-To: <5262b75e-8281-411d-2248-6be37fd52b85@wanadoo.fr>
X-Original-Sender: avi@scylladb.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@scylladb-com.20150623.gappssmtp.com;       spf=pass (google.com:
 domain of avi@scylladb.com designates 2a00:1450:400c:c0c::22c as permitted
 sender) smtp.mailfrom=avi@scylladb.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:31773
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/31773>



On 03/26/2017 07:50 PM, Vicente J. Botet Escriba wrote:
> Le 26/03/2017 =C3=A0 16:30, Avi Kivity a =C3=A9crit :
>> Consider this type-safe bool class:
>>
>>
>>   template <typename Tag>
>>
>>   class bool_class {
>>
>>       bool _value;
>>
>>   public:
>>
>>       explicit constexpr bool_class(bool v) noexcept : _value(v) {}
>>
>>       // more emulation of bool
>>
>>       static constexpr bool_class yes =3D bool_class{true};
>>
>>       static constexpr bool_class no =3D bool_class{true};
>>
>>   };
>>
>>
>> Compilation fails, I believe rightly, on clang, because by the time=20
>> yes and no are parsed, bool_class is an incomplete type, and=20
>> therefore not a literal type.  It could be worked around by making=20
>> yes and no functions.
>>
>> Should we make this legal?  It seems reasonable for types to offer=20
>> constants scoped under their own name, and if they are literal types,=20
>> that these constants can be constexpr.
>>
> It seems that you can do
>
>   template <typename Tag>
>
>   class bool_class {
>
>       bool _value;
>
>   public:
>
>       explicit constexpr bool_class(bool v) noexcept : _value(v) {}
>
>       // more emulation of bool
>
>       static const bool_class yes;
>
>       static const bool_class no;
>
>   };
>
>    constexpr bool_class bool_class::yes =3D bool_class{true};
>
>    constexpr bool_class bool_class::no =3D bool_class{true};
>
>

The compiler (clang) won't recognize yes and no as constexpr variables:


template <typename Tag>
class bool_class {
     bool _value;
public:
     explicit constexpr bool_class(bool v) noexcept : _value(v) {}

     // more emulation of bool
     constexpr bool operator=3D=3D(bool_class other) const {
     return _value =3D=3D other._value;
     }

     static const bool_class yes;
     static const bool_class no;
};

template <typename Tag>
constexpr bool_class<Tag> bool_class<Tag>::yes =3D bool_class{true};

template <typename Tag>
constexpr bool_class<Tag> bool_class<Tag>::no =3D bool_class{true};

static_assert(bool_class<int>::yes =3D=3D bool_class<int>::no);


bool_class2.cc:23:15: error: static_assert expression is not an integral=20
constant expression
static_assert(bool_class<int>::yes =3D=3D bool_class<int>::no);
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool_class2.cc:23:39: note: read of non-constexpr variable 'no' is not=20
allowed in a constant expression
static_assert(bool_class<int>::yes =3D=3D bool_class<int>::no);
                                       ^



--=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/afbe68ce-4606-06f0-e270-3dc4ff911bc4%40scylladb.=
com.

.
