220 30247 <CANPtknw9s4+tKU5SKbuPHRQKbbLdN1Xt8HGa_OiRa=_-rEiYbQ@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: enum_cast proposal
Date: Wed, 4 Jan 2017 20:28:38 +0100
Lines: 78
Approved: news@gmane.org
Message-ID: <CANPtknw9s4+tKU5SKbuPHRQKbbLdN1Xt8HGa_OiRa=_-rEiYbQ@mail.gmail.com>
References: <efdf2357-00a6-408a-8772-12f99135a880@isocpp.org>
 <64617344.0Ggdhq6B90@tjmaciei-mobl1> <d03061cc-7442-4e10-9313-a3394afa11e3@isocpp.org>
 <10490049.adee8Rr8fZ@tjmaciei-mobl1> <8747ddc6-c416-44b9-a8a0-b3a00f835c05@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 1483558127 16303 195.159.176.226 (4 Jan 2017 19:28:47 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Wed, 4 Jan 2017 19:28:47 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDRIBXXKZQERB2UZWXBQKGQEYBAGS3Q@isocpp.org Wed Jan 04 20:28:41 2017
Return-path: <std-proposals+bncBDRIBXXKZQERB2UZWXBQKGQEYBAGS3Q@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pf0-f199.google.com ([209.85.192.199])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDRIBXXKZQERB2UZWXBQKGQEYBAGS3Q@isocpp.org>)
	id 1cOrEm-0003TB-6M
	for gclcip-std-proposals@m.gmane.org; Wed, 04 Jan 2017 20:28:40 +0100
Original-Received: by mail-pf0-f199.google.com with SMTP id 127sf489202927pfg.5
        for <gclcip-std-proposals@m.gmane.org>; Wed, 04 Jan 2017 11:28:44 -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=I6f96mUFgp4vkspYJQvAYXghLpCwg8Pt6TWzXjBZn4o=;
        b=wTU9jtBZF8fYg1HeXYOeUYBRJBCmcnIfE05WuNFiQA/9g+DmsUwa/Kdg2NTtf4JBlt
         aeqSjBLUFTG0tnXC2RVNgW/PNYXXE5uWz/DxYbRnsNZlPDLpJko3jsCMMjHSIdOwJhuc
         zV+TT19Haq1ZyjU2fYasG+uGTlBMDsPLIJtjlntFaEUBu7K2tRX/u0qxOnsBfLy2pLBY
         kTlE3t2610E1K28QSRQO0YNi240ceqwRzkWa8BvB1GOxFGFgD7X7BwQXAKHAm1scvCFA
         YkMthrJ4FWE9fmHNLTHoVEsCuEGALWoJ18EqxJfCjC5og8VSZAMROEss16Ed7pi0NVAo
         Rgvg==
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=I6f96mUFgp4vkspYJQvAYXghLpCwg8Pt6TWzXjBZn4o=;
        b=pZbzsGBDPuVbYKOKwllBBY8yhngiknMSKvczfJZIen9/gPDqKj0agWdUMI0Y+SGFy4
         FcT3GudTFr2+UvRtpib2CDVXI257aEAeBKTT8jQd4eohDAlikSp5TfueEfUXWfmydOAa
         kRXE49ImUON4JvsLYg9CbVMuBi+GSb+jaQUmCXbhRWQJ7Fmt26Wmx0tqIwx1mELv7BV2
         R8n2M1jxy3jH6BHqFSohHJ55vKS2rRA5rFvLDLFUC7VJ/IuJkLB5aIs3RASuKjz94Qld
         0UqcpLjmYvMCTbXDtaqc6nefgxKzPzW8dxpqK1zhQYFvHnAS1YGV4oEh7fnJehqKXQjg
         +lWQ==
X-Gm-Message-State: AIkVDXJBfzU8oAo77I0+80KcIrEipUCNrZ2hwO2fYNHxZ102Wm4sJBEFS5drXxLNgds7JQ==
X-Received: by 10.99.123.73 with SMTP id k9mr32983568pgn.110.1483558123632;
        Wed, 04 Jan 2017 11:28:43 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.3.145 with SMTP id f17ls2350238otf.27.gmail; Wed, 04 Jan
 2017 11:28:42 -0800 (PST)
X-Received: by 10.31.88.1 with SMTP id m1mr24837781vkb.83.1483558120176;
        Wed, 04 Jan 2017 11:28:40 -0800 (PST)
Original-Received: from mail-ua0-x244.google.com (mail-ua0-x244.google.com. [2607:f8b0:400c:c08::244])
        by mx.google.com with ESMTPS id 44si18259996uau.47.2017.01.04.11.28.40
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Wed, 04 Jan 2017 11:28:40 -0800 (PST)
Received-SPF: pass (google.com: domain of peter.koch.larsen@gmail.com designates 2607:f8b0:400c:c08::244 as permitted sender) client-ip=2607:f8b0:400c:c08::244;
Original-Received: by mail-ua0-x244.google.com with SMTP id d5so30388315uag.0
        for <std-proposals@isocpp.org>; Wed, 04 Jan 2017 11:28:40 -0800 (PST)
X-Received: by 10.176.74.208 with SMTP id t16mr41650816uae.85.1483558119700;
 Wed, 04 Jan 2017 11:28:39 -0800 (PST)
Original-Received: by 10.103.11.5 with HTTP; Wed, 4 Jan 2017 11:28:38 -0800 (PST)
In-Reply-To: <8747ddc6-c416-44b9-a8a0-b3a00f835c05@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:c08::244 as permitted
 sender) smtp.mailfrom=peter.koch.larsen@gmail.com;       dmarc=pass (p=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:30247
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/30247>

For what it is worth, I have a small utility library for enumerations
(not to elegant, requiring a macro to define the machinery) where you
- among other stuff has two functions available that does what you
want

1) to_enum<Enum>(int x):
Returns the corresponding enum or an unspecified but valid value.
2) is_enum<Enum>(int x)
Returns true if x represents a valid enum.

Thus, If we have the definition

enum color
{
   red = 1,
   green = 200,
   blue = 3
};

is_enum<color>(100) would return false and to_enum<color>(100) would
return either red, green or blue.

This approach is in my opinion better than forcing some
error-reporting mechanism such as returning an optional or throwing an
error.

/Peter

On Wed, Jan 4, 2017 at 5:33 PM, Nicol Bolas <jmckesson@gmail.com> wrote:
>
>
> On Wednesday, January 4, 2017 at 11:16:17 AM UTC-5, Thiago Macieira wrote:
>>
>> On quarta-feira, 4 de janeiro de 2017 07:20:11 BRST m.ce...@gmail.com
>> wrote:
>> > Why do you want to use some compiler invented type instead?
>> > If enum_cast is a function in standard library, why can't it use another
>> > type from same library?
>>
>> Then it is a function in the standard library. That means it needs to be
>> based
>> on something else to perform the check, like reflection.
>
>
> There are plenty of functions in the standard library whose implementations
> rely on compiler-specific features. You wouldn't be able to implement most
> of the type traits without compiler intrinsic or some compiler-specific
> knowledge. You can't implement `type_info` or `initializer_list` without the
> compiler being involved. And so forth.
>
> This would simply be one more compiler-based construct. Once we get
> compile-time reflection, implementations could become platform-neutral
> (though the compiler-equivalent would probably still get faster).
>
> But how the function gets implemented ultimately has nothing to do with
> having it return `std::optional`.
>
>>
>>
>> --
>> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
>>    Software Architect - Intel Open Source Technology Center
>>
> --
> 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/8747ddc6-c416-44b9-a8a0-b3a00f835c05%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/CANPtknw9s4%2BtKU5SKbuPHRQKbbLdN1Xt8HGa_OiRa%3D_-rEiYbQ%40mail.gmail.com.

.
