220 13414 <6449799.vbEiQqPBsP@tjmaciei-mobl4> article
Path: news.gmane.org!not-for-mail
From: Thiago Macieira <thiago@macieira.org>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: atof(string_view) and others
Date: Tue, 30 Sep 2014 19:40:42 -0700
Lines: 73
Approved: news@gmane.org
Message-ID: <6449799.vbEiQqPBsP@tjmaciei-mobl4>
References: <b5f9aa7f-9b14-4403-b7e7-ff1a80e9b25b@isocpp.org> <2118839.fukHYmiT6S@tjmaciei-mobl4> <77247918-fbe5-418d-ab30-8b4b852dace9@isocpp.org>
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 1412131254 27337 80.91.229.3 (1 Oct 2014 02:40:54 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Wed, 1 Oct 2014 02:40:54 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCB4TK757YBRBL6TVWQQKGQEJCENT2I@isocpp.org Wed Oct 01 04:40:49 2014
Return-path: <std-proposals+bncBCB4TK757YBRBL6TVWQQKGQEJCENT2I@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-la0-f71.google.com ([209.85.215.71])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCB4TK757YBRBL6TVWQQKGQEJCENT2I@isocpp.org>)
	id 1XZ9qT-0001op-D6
	for gclcip-std-proposals@m.gmane.org; Wed, 01 Oct 2014 04:40:49 +0200
Original-Received: by mail-la0-f71.google.com with SMTP id gi9sf87553lab.2
        for <gclcip-std-proposals@m.gmane.org>; Tue, 30 Sep 2014 19:40:49 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:from:to:subject:date:message-id:user-agent
         :in-reply-to:references:mime-version:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe:content-type;
        bh=fAPvAttxKSg95fUxHyJCj57ObiiycG2heQzT4z4NC4k=;
        b=d+ex5Xp9RXjUyJcRH6u2MnffHOiR7JGQDo16PZhyVVY7tofqFGDbVPJnLT8Uj/ZijV
         YMGKkqDM8Hli4ySKFRMj2YCfOenRq475tga5aXm7/rYmqG/BBNNiRP3QOhWpSOpQwbw7
         NqppTLq2Cs4IFZqMQAbcD0o4HrpoeuB+cFbAxPxUQW2v7txRdIrn/Ey1Vrq4dWswVbUo
         6/t85H75uJ0JDXddyHRlM4cn+4ot13aBhx6wSI+l4ZunQvchFgURzJqDqkfLPdG4p0MO
         QgoCoYnJ/ofWqhtbRf0scPXLuZdwjlQ9V0i5jQP185qAOs+nUOMKVgXkdBIeRsA9z4jx
         F3dQ==
X-Gm-Message-State: ALoCoQmkoRhVVgDZuzfWAzD2uyMDLm/rFsOtOrhJbgNzdQ/v6Ermu5qkV9IWRzP2zshydyazomC5
X-Received: by 10.112.168.225 with SMTP id zz1mr1074042lbb.8.1412131249193;
        Tue, 30 Sep 2014 19:40:49 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.152.5.38 with SMTP id p6ls108511lap.72.gmail; Tue, 30 Sep 2014
 19:40:46 -0700 (PDT)
X-Received: by 10.152.1.74 with SMTP id 10mr41243701lak.43.1412131246864;
        Tue, 30 Sep 2014 19:40:46 -0700 (PDT)
Original-Received: from gondolin.macieira.info (gondolin.macieira.info. [78.47.120.188])
        by mx.google.com with ESMTP id lk1si25281976lac.4.2014.09.30.19.40.46
        for <std-proposals@isocpp.org>;
        Tue, 30 Sep 2014 19:40:46 -0700 (PDT)
Received-SPF: pass (google.com: domain of thiago@macieira.org designates 78.47.120.188 as permitted sender) client-ip=78.47.120.188;
Original-Received: from tjmaciei-mobl4.localnet (unknown [IPv6:2601:7:1c80:58c:30e7:a180:29c0:1e9d])
	by gondolin.macieira.info (Postfix) with ESMTPSA id 130BC11BA60
	for <std-proposals@isocpp.org>; Tue, 30 Sep 2014 19:40:45 -0700 (PDT)
User-Agent: KMail/4.14 (Linux/3.11.10-21-desktop; KDE/4.14.0; x86_64; ; )
In-Reply-To: <77247918-fbe5-418d-ab30-8b4b852dace9@isocpp.org>
X-Original-Sender: thiago@macieira.org
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of thiago@macieira.org designates 78.47.120.188 as permitted sender) smtp.mail=thiago@macieira.org
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>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://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>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:13414
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/13414>

On Tuesday 30 September 2014 19:13:40 Matthew Fioravante wrote:
>    1. You lose the ability to check specific error conditions like ERANGE.
>    I'm not sure how useful this is as I've never done an ERANGE check on a
>    number parse but that functionality is available with the legacy
> functions. 

I don't see how that relates to the optional. In fact, I don't see how you 
came to this conclusion.

If the parsing failed, the function will return a disengaged optional and set 
errno to ERANGE or EINVAL. If it worked, it will return an engaged optional 
and errno is unspecified.

That's much better than strtoul, which returns zero when it fails and that's 
undistinguishable from a successful parsing of a number zero.

> 2. If you want to enable a throwing interface by just calling
>    optional::value() and letting it throw on error, you'll be throwing a
>    std::bad_optional_access. It would be better to throw an exception class
>    which is more specifically related to parsing numbers and has information
> on what went wrong.

I again don't understand why that is a problem. What's stopping you from 
throwing an exception if the returned optional is disengaged?

> Also I think its better to use a string_view& for the tail instead of a
> char**. The tail string itself is not null terminated so using a character
> pointer with which we usually associate null termination is a big
> misleading. Also converting the char** endptr back into a string_view is
> clumsy and error prone as it will call strlen() on the non-null terminated
> buffer unless the programmer carefully computes and specifies the new
> length.

Yeah, that makes sense. I was pondering that when I wrote the suggestion, but 
didn't go through with it.

> I'd also like to try to make the integer conversions constexpr so setting
> errno is out.

Forget it. Non-inline functions can't be constexpr and those functions 
shouldn't be inline.

At best, they could be an inline wrapper to the real parser, such as:

template <typename T> optional<T> 
to_integral(string_view str, int base = 10, string_view *tail = nullptr)
{
	typedef typename conditional<is_unsigned<T>::value,
		unsigned long long, long long>::type U;
	U min = numeric_limits<T>::min();
	U max = numeric_limits<T>::max();
	optional<U> result =
		to_integral_helper(str, base, tail, min, max);
	return result ? optional<T>(result.value()) : optional<T>();
}

This will require two only out-of-line functions, one for long long (for 
signed conversions) and one for unsigned long long (for unsigned ones).

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358

-- 

--- 
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

.
