220 29248 <D44CA73E-022C-4803-8F52-F644679B996E@gmx.de> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Daniel Frey <d.frey@gmx.de>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: explicit conversion from enum class to its
 underlying type
Date: Sun, 30 Oct 2016 02:40:19 +0200
Lines: 74
Approved: news@gmane.org
Message-ID: <D44CA73E-022C-4803-8F52-F644679B996E@gmx.de>
References: <4afed494-4fc5-4b06-b3e7-255aa293068e@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0 (Mac OS X Mail 10.1 \(3251\))
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-Trace: blaine.gmane.org 1477788054 20766 195.159.176.226 (30 Oct 2016 00:40:54 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Sun, 30 Oct 2016 00:40:54 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDYYLHN44AERB5MC2XAAKGQEEPV6JSY@isocpp.org Sun Oct 30 02:40:49 2016
Return-path: <std-proposals+bncBDYYLHN44AERB5MC2XAAKGQEEPV6JSY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-wm0-f72.google.com ([74.125.82.72])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDYYLHN44AERB5MC2XAAKGQEEPV6JSY@isocpp.org>)
	id 1c0eAd-0001IV-37
	for gclcip-std-proposals@m.gmane.org; Sun, 30 Oct 2016 02:40:19 +0200
Original-Received: by mail-wm0-f72.google.com with SMTP id i128sf49734150wme.2
        for <gclcip-std-proposals@m.gmane.org>; Sat, 29 Oct 2016 17:40:22 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=from:content-transfer-encoding:mime-version:subject:date:references
         :to:in-reply-to:message-id: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=34kNE1vy9lGFL3NbmTq6enuTGTuAO4Yq5StsUGTIq7w=;
        b=Os9soYA+HYZ8z707LDh+xxxND+6jr7+vXhMawVfit/bM84ZRkehkXKi4K+3RdoKTdq
         pfLGaLJkBFpHDJo3oQhg8b8JDr5qGZzC8QcSS577/2/0F11OVbuoJfj1MhHBbRHlHUb9
         qwLhSYoR7VO+ud6ZspjCrV8UVGQWkw8oP7zQdM2XIypC9fzNtdPBxmqeCXEDXcbV35Vl
         K3ZUc9/9QE27D9anZ9I3qQ6720wFPJZFcd+bY6ZZN0OmDfdEjSSrT2knzG9TlqdBYSEg
         2/qTZ1QPSa740jhydpIWn8cMm9CQMdQgAPL+0iLfnVCE58y6In3Tx8Aks/spETksh/4y
         5X6A==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:from:content-transfer-encoding:mime-version
         :subject:date:references:to:in-reply-to:message-id: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=34kNE1vy9lGFL3NbmTq6enuTGTuAO4Yq5StsUGTIq7w=;
        b=RXIPZyZVGfogYiQZ7jPaRexfabnaxCaqOM9auCbP60tXvf2Y9QcT1keVEJqe23R/Gz
         crUY8SNRiviNTDvcZL8obR01j75thP26xE3b18EsSwvCdx2+zBvic2uRqjf/+HK/1JQi
         jZrd0sxoKULyokPfI02OE2q236Z99ctjNHa7xRnfOT90IrTOVFYgtY54oHQzrFxwuqaP
         ND/PvvEtOLqaUPZTid+UACEkXy9YRh+GCj48yy+okg4KJm//Lz/WZtEITKZrfvdK05n2
         VC9HM5Qqmu3CQ+Iizktte/9hvUoVsYbPQyrsYQTKaJOCobX+UkLlSUT3KMFe/rCuugC/
         qSQ 
X-Gm-Message-State: ABUngvcZHU2GTdITm0BQfYAUOg25v2+eLgy1HJzn5APlgxHTD3swZ6YU2tIE8utK9wBWmg==
X-Received: by 10.28.198.10 with SMTP id w10mr368796wmf.7.1477788022004;
        Sat, 29 Oct 2016 17:40:22 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.48.195 with SMTP id w186ls984194wmw.24.gmail; Sat, 29 Oct
 2016 17:40:21 -0700 (PDT)
X-Received: by 10.194.116.225 with SMTP id jz1mr17167636wjb.224.1477788021036;
        Sat, 29 Oct 2016 17:40:21 -0700 (PDT)
Original-Received: from mout.gmx.net (mout.gmx.net. [212.227.17.21])
        by mx.google.com with ESMTPS id h4si22615233wjl.68.2016.10.29.17.40.20
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Sat, 29 Oct 2016 17:40:21 -0700 (PDT)
Received-SPF: pass (google.com: domain of d.frey@gmx.de designates 212.227.17.21 as permitted sender) client-ip=212.227.17.21;
Original-Received: from fiasko.fritz.box ([84.175.87.245]) by mail.gmx.com (mrgmx103)
 with ESMTPSA (Nemesis) id 0LjJCt-1cbN6J2MU3-00dXAs for
 <std-proposals@isocpp.org>; Sun, 30 Oct 2016 02:40:20 +0200
In-Reply-To: <4afed494-4fc5-4b06-b3e7-255aa293068e@isocpp.org>
X-Mailer: Apple Mail (2.3251)
X-Provags-ID: V03:K0:dsnn3zirrIKC+O+eN7jQ2SdXprZHfvxx9LZQhrXPiZXCkZSTdcr
 R67HsMwz18nh2oHqB3STOwZcBE2vD7E5yWG37VkoSyPFZB1/4XftUaokhmtqH6BxWgYET+3
 lhA1Q3Gr8STUrN3zpSq3qe7MVJ/bqEaYlK3f5kNH/gAGfD535sEgoAY1QCRHuZc6FZlnRTt
 H/wlVyokHiWJ1pRE/pFvQ==
X-UI-Out-Filterresults: notjunk:1;V01:K0:fqM7+vpQkmU=:5W7XxxbgFqp/H9MtOCcxkK
 pfHWh6wm8pGJCsBFMu34Qc1bbIFtZQOvBdaPE1VcoaQp/SLG+nnwXO4fw6hn9Wok0fG6x1d44
 Q0BoqyiNNEvCn3ukNrjkVBlMfYkBTaIrldZhlrcRrDAILTWD12Ls/P1Oacbq4vWdGIb1jPjIT
 3mRt3780QWDgd5tzguTR0lBMdNLNEyq4OwPASvoHHXakfcTdtAkdmHccKqCgIQy/p9Qfpj62k
 RzqgdcBGhCqt4Xlt7YJkxrfzFdPHDX9mGj9voH6i7oi1JJYthdHRzfVa0Cs/Q+y6aWjwtX1Rz
 UuskmFH8nn2TI5abyrOQ8fslvAXlFZWxdPFDT0uaGVXr703aBds3G21K7P1L3tDEw4+QYScza
 PEhHOcjtBJ7a40szWOyhf7+BetTTpCkgcMJ6YbDBvdx7DXGMwh07MkD9UJQ8ouAdMHgdQtD1A
 bCe527mw043aUNaht4aGAdi/UuhuSuu1Ha0CDon0I7bEY5tzGCHl4KfT8gW8l3Vn9HgBxOjf2
 MUtYlKE9yu3tw5RN4Ck6mzTRcDbGKYDTfYPWIOUJ8GkKDdCB+Cd2K9FyP1JTTAUXrr5uBcdhh
 JlgfkLzBkii1ZlOjZwrkwpSxv11H5vhejohSGUh40+6sIu77QkpYu+yY/Wr1auLyoMs8dytjF
 3kOnzDMp53yc6Pi6kz9KsmtXc2j9VG91tj7CsxLlabcDjCgENDOzjAUb6nBxStce/1lVuRLfU
 BiBtiFCaoZ30OEmcCVYUquWFnaGAhmnQQdIuB8Vt7UUbdtScVC+1yXD/ueNt1dacH2XKBNnE 
X-Original-Sender: d.frey@gmx.de
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of d.frey@gmx.de designates 212.227.17.21 as permitted sender) smtp.mailfrom=d.frey@gmx.de
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:29248
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/29248>

> On 29 Oct 2016, at 23:51, Andrzej Krzemie=C5=84ski <akrzemi1@gmail.com> w=
rote:
>=20
> I do not know of any practical use of explicit conversion operators excep=
t for the contextual conversion to bool. And it is only this conversion tha=
t I am interested in.

If all you need is boolean conversion, how about

  template<typename E>
  constexpr std::enable_if_t<std::is_enum_v<E>,bool> is(E e) noexcept
  {
    return e =3D=3D E(true);
  }

to be used like

  if(is(skipUnused)) { ... }

Instead of is(), you could also use operators, e.g.

  +skipUnused

or

  !skipUnused

(the latter might actually be sensible, but inconsistent if skipUnused itse=
lf is not convertible to bool)

There is a caveat with using boolean enums, i.e., if the user writes:

   enum class SkipUnused { Yes, No };

(Note the order of the values).

There is no way for us to detect this, unless we explicitly would expect a =
value called Yes (or should it be YES or TRUE?) and change the above to

  template<typename E>
  constexpr std::enable_if_t<std::is_enum_v<E>,bool> is(E e) noexcept
  {
    return e =3D=3D E::Yes;
  }

> Do you think, it would be acceptable to add such explicit conversion to t=
he underlying type, for enum classes?

If anything, it should be explicit for a type, not implicit for all enums. =
Maybe allowing something like:

  enum class SkipUnused { No, Yes };
  explicit constexpr SkipUnused::operator bool() =3D default;

or

  explicit constexpr SkipUnused::operator bool() noexcept { return *this =
=3D=3D Yes; }

where *this refers to the enum value and Yes is in the scope of SkipUnused,=
 hence no SkipUnused::Yes is needed. Something like a "member" of an enum c=
lass. This might also allow other conversions (explicitly), making a possib=
le proposal usable in more contexts. I'm not really sure it is worth it, th=
ough.

--=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/D44CA73E-022C-4803-8F52-F644679B996E%40gmx.de.

.
