220 30464 <CANPtknwzegDLrDFrMoc5U9Z1y52PBZAC2BtQ=iS6Eur2A6f7NQ@mail.gmail.com> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Peter Koch Larsen <peter.koch.larsen@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Re: enum_cast proposal
Date: Tue, 10 Jan 2017 18:01:45 +0100
Lines: 92
Approved: news@gmane.org
Message-ID: <CANPtknwzegDLrDFrMoc5U9Z1y52PBZAC2BtQ=iS6Eur2A6f7NQ@mail.gmail.com>
References: <efdf2357-00a6-408a-8772-12f99135a880@isocpp.org>
 <57258435-5d82-48e7-8ed7-3c38682418fb@isocpp.org> <a35d9f20-9e4f-4789-b7e5-5cb08f99e6bd@isocpp.org>
 <58750288.5080004@gmail.com> <e85f0d46-f244-42fe-a276-b4e18b3d627f@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
X-Trace: blaine.gmane.org 1484067716 24104 195.159.176.226 (10 Jan 2017 17:01:56 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 10 Jan 2017 17:01:56 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDRIBXXKZQERB65G2TBQKGQEFWD4TKY@isocpp.org Tue Jan 10 18:01:52 2017
Return-path: <std-proposals+bncBDRIBXXKZQERB65G2TBQKGQEFWD4TKY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-yb0-f199.google.com ([209.85.213.199])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDRIBXXKZQERB65G2TBQKGQEFWD4TKY@isocpp.org>)
	id 1cQznr-000535-I9
	for gclcip-std-proposals@m.gmane.org; Tue, 10 Jan 2017 18:01:43 +0100
Original-Received: by mail-yb0-f199.google.com with SMTP id l23sf406579226ybj.6
        for <gclcip-std-proposals@m.gmane.org>; Tue, 10 Jan 2017 09:01:48 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=mime-version:in-reply-to:references:from:date:message-id:subject:to
         :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=ue/x5jwy+JRnQIfFz0G9kAaTxgPPMw0YlKHaIQZIlf0=;
        b=1ck2G7ZVbpvgQLofs+AZZIsUYKlKjs7EG5fVF913z4PWW72Fxd1wMGePhEMK0ue3oC
         j17Uv1VANR2z7TztqwDts3Cz1ZnnUPvnJLyJLfTLeRDsppCxi2e8rzVXadffAENA5nth
         feZhK/774X7OVnps3jKvVbyCEaW0pfpm06tTIufZrk95HM7b8yrYRjRrtd5fnqDaIjN1
         6jhL4yGgjlzWCBqomPE919AcuUI5ZBXMDYTfBXFvLK5XGRDe03Rwuk9Vy+hi1JlESJnt
         ODGGLNh+EcbINOaQT+FQwsy9EjJiwpmlxZVi+hkAv5QwVpEMDUoLX71s46bqy5jNp3nn
         fKrQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:mime-version:in-reply-to:references:from:date
         :message-id:subject:to: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=ue/x5jwy+JRnQIfFz0G9kAaTxgPPMw0YlKHaIQZIlf0=;
        b=WRMW4LEAvAGTDf2h7d0Li+ChQlCepCCyiWNqc04I9jSm3We/7NJTgOTdyl4awFFYED
         Bm463Ok6ttGThSpN1oggwR97PTnFX6Z+IA1oITSNZCMn0Ihx+nZIETjloKZlF5hIorON
         OLxb/yPoNj1LQnBvhXIYQdF8Ta35oe5qy0XEVJvPy+ia8Od5/5+5XyT2c/To+Fo9NN/Q
         JOR8MzONXaNQ9d8iWkD7VHeEH/phSKdAB7jhzuka3wUAgsSgZyctSizP7YfQ3XJTpDbi
         lONV7jvZpeRYcGXUPp/bJJ9ao8INQR3UdieizQVQtD1fS/bD1YptxY9QsBHAEWUT+Yoz
         Ygww==
X-Gm-Message-State: AIkVDXI26CP6sgc3GtScCRuQjFn6LDZeXZXZyz2ASuW8xaMKmIUpdD7oTlIh69Qigz47fg==
X-Received: by 10.129.175.36 with SMTP id n36mr245836ywh.30.1484067707867;
        Tue, 10 Jan 2017 09:01:47 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.17.154 with SMTP id v26ls1998650otf.0.gmail; Tue, 10 Jan
 2017 09:01:47 -0800 (PST)
X-Received: by 10.31.232.198 with SMTP id f189mr1861325vkh.85.1484067706997;
        Tue, 10 Jan 2017 09:01:46 -0800 (PST)
Original-Received: from mail-vk0-x22d.google.com (mail-vk0-x22d.google.com. [2607:f8b0:400c:c05::22d])
        by mx.google.com with ESMTPS id 91si655764uan.100.2017.01.10.09.01.46
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 10 Jan 2017 09:01:46 -0800 (PST)
Received-SPF: pass (google.com: domain of peter.koch.larsen@gmail.com designates 2607:f8b0:400c:c05::22d as permitted sender) client-ip=2607:f8b0:400c:c05::22d;
Original-Received: by mail-vk0-x22d.google.com with SMTP id t8so30707211vke.3
        for <std-proposals@isocpp.org>; Tue, 10 Jan 2017 09:01:46 -0800 (PST)
X-Received: by 10.31.73.66 with SMTP id w63mr1928011vka.106.1484067706410;
 Tue, 10 Jan 2017 09:01:46 -0800 (PST)
Original-Received: by 10.103.11.5 with HTTP; Tue, 10 Jan 2017 09:01:45 -0800 (PST)
In-Reply-To: <e85f0d46-f244-42fe-a276-b4e18b3d627f@isocpp.org>
X-Original-Sender: peter.koch.larsen@gmail.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@gmail.com;       spf=pass (google.com: domain of
 peter.koch.larsen@gmail.com designates 2607:f8b0:400c:c05::22d as permitted
 sender) smtp.mailfrom=peter.koch.larsen@gmail.com;       dmarc=pass (p=NONE
 sp=NONE dis=NONE) header.from=gmail.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:30464
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/30464>

Nicols proposal is very similar to mine, except that I do not have an
enum_cast_opt. And I do not really see a reason to have this option.
I believe that noone would use that option. Why write

std::optional<Enum> e = enum_opt_cast<Enum>(x);
if (e)
{
    normal processing
}
else
{
    handle conversion failure
}

instead of

if (is_enum<Enum>(e))
{
    Enum e = to_enum<Enum>(x);
    normal processing
}
else
{
    handle conversion failure
}
?
Things to consider when implementing:

What do you do if you have an enum with some same_valued elements
(enum E { ea = 0, eb = 0})? My implementation forbids this.
It could also be useful to have an ordinal position:

enum E
{
 ea = 1000,
 eb = -1000,
 ec = 32
};

static_assert(to_ordinal<Enum>(ea) == 0,"??");
static_assert(to_ordinal<Enum>(eb) == 1,"??");
static_assert(to_ordinal<Enum>(ec) == 2,"??");

I have found uses for this in my code.

/Peter

On Tue, Jan 10, 2017 at 5:20 PM, Nicol Bolas <jmckesson@gmail.com> wrote:
> On Tuesday, January 10, 2017 at 10:49:34 AM UTC-5, Matthew Woehlke wrote:
>>
>> On 2017-01-10 10:31, m.ce...@gmail.com wrote:
>> > For sure baseline API must be exception-free.
>> > Do we really want to additionally support API that throws on error?
>>
>> I *really* hope we can use something like std::expected; this is a
>> textbook use case for that, and makes it trivial to have a
>> throw-on-error API (just grab the value from the expected without
>> checking it first; this will turn around and throw if the value is not
>> there).
>
>
> But the fact that it's throwing the wrong exception makes it useless from an
> exception handling perspective. If you want an API to throw, you want it to
> throw a meaningful exception type, not the same kind of exception that 20
> other APIs throw.
>
> So yes, we really do need to support a throwing API. I say we should have 3
> interfaces:
>
> * `can_enum_cast`: specifies whether the value is in an enumeration. This is
> where the compiler magic comes in, of course.
> * `enum_cast`: Converts to the enumeration type, or throws if it cannot.
> * `enum_cast_opt`: Returns an `optional<Enum>`.
>
> The first allows you to build whatever tools you want. But we have default
> tools for C++'s standard mechanisms: throwing and `optional`.
>
> --
> 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
> email 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/e85f0d46-f244-42fe-a276-b4e18b3d627f%40isocpp.org.

-- 
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 email 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/CANPtknwzegDLrDFrMoc5U9Z1y52PBZAC2BtQ%3DiS6Eur2A6f7NQ%40mail.gmail.com.

.
