220 2392 <CAHOE3yfsCHFTgcgSt+ytzqx_V5GRWrbobO5hSrd8nDQZ2NC98A@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: Daniel James <dnljms@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: optional<T>, one more value or two states
Date: Mon, 28 Jan 2013 09:07:23 +0000
Lines: 63
Approved: news@gmane.org
Message-ID: <CAHOE3yfsCHFTgcgSt+ytzqx_V5GRWrbobO5hSrd8nDQZ2NC98A@mail.gmail.com>
References: <CAGsORuC1ADLXgS9AZZiTo=knmhT-+9Yj+82v+b8Nq_WJpb1fOg@mail.gmail.com>
	<CAFk2RUZyZA0LLUNuVNcnibv+qBF69jMytWQL3tkp-Kp21oEU+A@mail.gmail.com>
	<CAGsORuB-Qkx-LaXj2jO4yLrwN524kiJONSg7oczkQZHYaJ6ASg@mail.gmail.com>
	<d360a952-28ae-4d57-bb20-96fd08725a0b@isocpp.org>
	<CAPXezF_JggyMaXjGS9r1EAGVu1NhQBEyGEXB8tKmVqgFfjoCeg@mail.gmail.com>
	<CALJmx62zJd_tZ=XYL7TaSkRN6432SObwH9YS87eLX-h_pMuFYQ@mail.gmail.com>
	<CAOHCbivAK0ypmMhrhJ3zKpF8BHu3h_rUNzeao-HnibJkC-qqGw@mail.gmail.com>
	<ef15483d-5665-4da3-9b79-aefd3c725e1b@isocpp.org>
	<CAOHCbithEUfm+w_06V71=hpkqGUJvqiyDFqD7fesfgLy4uUieg@mail.gmail.com>
	<2c2b9360-8e5e-422d-9b85-124bd947b004@isocpp.org>
	<123b9f65-12fd-4285-bb6c-cfee383d4552@isocpp.org>
	<CAHOE3yeF6KBBz7a1uw58oA3Shh_w0HFGiLtUGEd4ntJAKBauew@mail.gmail.com>
	<CAA7U3HNDqV-j9wU0fw9kBwkoD6TiSmuZZhx1JCg3CNztYVMqqA@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
X-Trace: ger.gmane.org 1359364045 18090 80.91.229.3 (28 Jan 2013 09:07:25 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Mon, 28 Jan 2013 09:07:25 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCGPB3VVUAGBBTP7TCEAKGQENAYLKSA@isocpp.org Mon Jan 28 10:07:44 2013
Return-path: <std-proposals+bncBCGPB3VVUAGBBTP7TCEAKGQENAYLKSA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-wg0-f69.google.com ([74.125.82.69])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCGPB3VVUAGBBTP7TCEAKGQENAYLKSA@isocpp.org>)
	id 1Tzkgp-0000zm-5u
	for gclcip-std-proposals@m.gmane.org; Mon, 28 Jan 2013 10:07:43 +0100
Original-Received: by mail-wg0-f69.google.com with SMTP id fn15sf2601007wgb.4
        for <gclcip-std-proposals@m.gmane.org>; Mon, 28 Jan 2013 01:07:25 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=x-received:x-beenthere:x-received:x-received:received-spf
         :mime-version:x-received:in-reply-to:references:date:message-id
         :subject:from:to:x-original-sender:x-original-authentication-results
         :reply-to:precedence:mailing-list:list-id:x-google-group-id
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe
         :content-type;
        bh=ARYpvfSan3/Fiykrfj9D4ZpXm3ftoUnqcKQwuqH8B5M=;
        b=rjjYGlJFVWIsVdR9L01RUWLNGS5k7pacsCej0BSMeLpzHGif9zrcAN37vpUk9VbHML
         MQh9Oeyb/CpKN/A8xK2zfYDyjY7WYFt1dqIu+0fnQzsfLF9X48ajOPcekZkkTia7k1um
         0g/1kDxqPx9Tifsbn2GD5S3ABfVLUAdYYCBwDlkRcJ1zOFk17MC/hKim3B2NIM2Jdu09
         q8qfih6wfuXoLV1Uee89S3+e8VQcMq5Ta7qSQaFpPag15oU3r9XkRRf/SzwwF2Thq5dL
         ARq+tXNL/xsUh4rTrITG7N1hkJZUK1PVt9znwLk81rRhanm1l 
X-Received: by 10.180.100.74 with SMTP id ew10mr1452837wib.7.1359364045251;
        Mon, 28 Jan 2013 01:07:25 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.180.14.105 with SMTP id o9ls860529wic.3.gmail; Mon, 28 Jan
 2013 01:07:23 -0800 (PST)
X-Received: by 10.180.101.99 with SMTP id ff3mr8333520wib.21.1359364043858;
        Mon, 28 Jan 2013 01:07:23 -0800 (PST)
X-Received: by 10.180.101.99 with SMTP id ff3mr8333518wib.21.1359364043836;
        Mon, 28 Jan 2013 01:07:23 -0800 (PST)
Original-Received: from mail-we0-x235.google.com ([2a00:1450:400c:c03::235])
        by mx.google.com with ESMTPS id fy9si3130273wjb.199.2013.01.28.01.07.23
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Mon, 28 Jan 2013 01:07:23 -0800 (PST)
Received-SPF: pass (google.com: domain of dnljms@gmail.com designates 2a00:1450:400c:c03::235 as permitted sender) client-ip=2a00:1450:400c:c03::235;
Original-Received: by mail-we0-f181.google.com with SMTP id t44so1334099wey.12
        for <std-proposals@isocpp.org>; Mon, 28 Jan 2013 01:07:23 -0800 (PST)
X-Received: by 10.194.78.236 with SMTP id e12mr19659022wjx.32.1359364043677;
 Mon, 28 Jan 2013 01:07:23 -0800 (PST)
Original-Received: by 10.194.46.7 with HTTP; Mon, 28 Jan 2013 01:07:23 -0800 (PST)
In-Reply-To: <CAA7U3HNDqV-j9wU0fw9kBwkoD6TiSmuZZhx1JCg3CNztYVMqqA@mail.gmail.com>
X-Original-Sender: dnljms@gmail.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of dnljms@gmail.com designates 2a00:1450:400c:c03::235 as permitted
 sender) smtp.mail=dnljms@gmail.com;       dkim=pass header.i=@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: <http://groups.google.com/a/isocpp.org/group/std-proposals/post?hl=en>,
 <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?hl=en&topic=25838>,
 <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe?hl=en>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe?hl=en>,
 <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:2392
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/2392>

On 27 January 2013 21:39, Olaf van der Spek <olafvdspek@gmail.com> wrote:
> On Sun, Jan 27, 2013 at 9:27 PM, Daniel James <dnljms@gmail.com> wrote:
>> But it could easily be changed to meet them. 'Optional' is a container
>> (in the wider sense) with a maximum of 1 element. It is not in any
>> sense a pointer. In fact, std::optional probably should meet the basic
>> container requirements, as it would allow it to be used with generic
>> functions.
>
> It's a bit like static_vector with max_size 1, true. Though I'm not
> sure what supporting a container interface really buys you.

It depends on how well the range algorithms works out. They might
provide a more elegant way to manipulate optionals than if statements,
similar to how a range algorithm can be more elegant than a for loop.
This has worked out well in other languages, for example in Scala:

http://www.scala-lang.org/api/current/index.html#scala.Option

It also allows for use in generic code, the classic example is
'flatten', which flattens a single layer of a nested container, e.g.
from vector<vector<int>> to vector<int>. If optional works as a range
then 'flatten' will also work for vector<optional<int>> resulting in a
vector that only contains the elements with values. This is
particularly useful when combined with 'transform', allowing you to
use a function which returns many values or optional values and then
flatten them into a single list. In functional languages this is
called 'flatMap' and is considered a fundamental building blocks. In
C++ such a function would have to have a different name (not sure
what, 'flat_transform' feels a bit off), and perhaps not be quite so
useful, but the point is that seeing optionals as ranges has a proven
track record.

>>>  I think using operator* is natural.
>>
>> But it does currently imply certain semantics. The current situation
>> is that if you write:
>>
>>     auto x = y;
>>     *x = 2;
>>
>> Then for any standard type this is equivalent to assigning '2' to
>> whatever y points to. By using 'operator*' in std::optional, the
>> semantics of 'operator*' will be changed. That shouldn't be done
>> lightly, especially since with templates and 'auto' a type is defined
>> by the functions and operators that it supports..
>
> Isn't that more of a problem with the constructor than with operator*?

The structural type implies certain behaviour. When a class has
'operator*' it should be expected to behave like a pointer, or if you
prefer, a proxy. Which implies that the constructor should also behave
like a pointer/proxy.

-- 

--- 
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To post to this group, send email to std-proposals@isocpp.org.
To unsubscribe from this group, send email to std-proposals+unsubscribe@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.



.
