220 33242 <c395066b-4639-3abb-b923-63040818904c@palves.net> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Pedro Alves <pedro@palves.net>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Proposal "is_constexpr"
Date: Tue, 18 Jul 2017 14:20:34 +0100
Lines: 95
Approved: news@gmane.org
Message-ID: <c395066b-4639-3abb-b923-63040818904c@palves.net>
References: <803a7b14-34c2-4ee6-8c5a-154532143a6e@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"
Content-Transfer-Encoding: quoted-printable
X-Trace: blaine.gmane.org 1500384043 18281 195.159.176.226 (18 Jul 2017 13:20:43 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 18 Jul 2017 13:20:43 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
 Thunderbird/45.4.0
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDRZRD7VU4BBBJMWXDFQKGQER3BXAQA@isocpp.org Tue Jul 18 15:20:36 2017
Return-path: <std-proposals+bncBDRZRD7VU4BBBJMWXDFQKGQER3BXAQA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-lf0-f70.google.com ([209.85.215.70])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDRZRD7VU4BBBJMWXDFQKGQER3BXAQA@isocpp.org>)
	id 1dXSQU-0004Dk-C0
	for gclcip-std-proposals@m.gmane.org; Tue, 18 Jul 2017 15:20:34 +0200
Original-Received: by mail-lf0-f70.google.com with SMTP id g25sf2752658lfh.8
        for <gclcip-std-proposals@m.gmane.org>; Tue, 18 Jul 2017 06:20:40 -0700 (PDT)
ARC-Seal: i=2; a=rsa-sha256; t=1500384039; cv=pass;
        d=google.com; s=arc-20160816;
        b=W66hLmP+TPMQKKQpQdBCJMiDKcy/+yit/EcmGoNuMIlg+ODvVvqOkYhoSpkr/CR+4k
         PAOsDIFHAJjg7tJVipHWsQsP5/6Ry5D40XtXqgNi9Q+cMrLKg8G+rCWy0MQsllllw3kV
         /+cMMGobUQkNviYdiKKHm+VYrHz5ZYe67VbQDKVDS0KVI1Vzw2dl/d3n0U0GOSq0bKbi
         GLnvqxRp6yWggThXXzkSGK3+Hl1XexdJE9v1QCuUzbkpe/Yc0Wvo5W02RQgbsZYgdt0M
         0Z2j+n7TkVJkNWKnKuQMtiNieyIDIvdIYxYWjZfVV+T4/Adg0IZDAuTJDZFzPjv7woHA
         6D2w==
ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post
         :list-id:mailing-list:precedence:reply-to:content-transfer-encoding
         :in-reply-to:mime-version:user-agent:date:message-id:references:to
         :subject:from:arc-authentication-results:arc-message-signature
         :dkim-signature:arc-authentication-results;
        bh=4p7IOwBs2JIRzN8FUV9+6lIz0rWPMjBRfDFCPN8usF0=;
        b=kCmES5jR4fDEO1mTQlT2ek8t5N7ND8tSzeGVUiB38VIzqkb2qJIMDnGP/sqT5ARVHt
         cGD3yaqML2oHWt9Gtd531T+lqNtrAO+/qzSibdj15/7OX203IWEGR0aRPFevJnNedtnC
         8ekwJZinKoh8MDnnXLnBN4bM68/w44C276ksOehv182QUGtusMZlQYeeDcElU2zltIs0
         pIRlTal002X/YhS+De7wMGoN1XPajQjPh44SS7VCKLN3oo+WIo6evlXe+zgXx0LRPP8m
         KBzH5UiQ/KenACuj0Gfis/Pc/f+iMOErkwr+HTGZpuHghJhaQ/3JO8RNrLQCzzi3nk/Q
         p 
ARC-Authentication-Results: i=2; mx.google.com;
       spf=pass (google.com: domain of alves.ped@gmail.com designates 209.85.128.172 as permitted sender) smtp.mailfrom=alves.ped@gmail.com
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=from:subject:to:references:message-id:date:user-agent:mime-version
         :in-reply-to:content-transfer-encoding:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=4p7IOwBs2JIRzN8FUV9+6lIz0rWPMjBRfDFCPN8usF0=;
        b=WwrVlt3cwinV542dLokqNVnfuW/6Y4d7sb7SkD5emnZvIgafCyWgrbYKD2IVCbR48z
         77Acfr4hvq2ziIIYrM0g2QRCfYcSKij/BtwFKwAOwbF/0jsHXu+VF5ddJq94Go1M5T7t
         Mc82K+5AfRnpZDiieXZNlDhAn1A1nTBW944tDAzeblIhWTVJu50bBWuuxFOWYVixATrz
         rKp4xWb/SgA3qDhT14GD0sxrVy1km5+bus21fU4XfZut1yynharHMaodXuhp+o9u30na
         /N90opiifKVPY+CKUh0uF2OBHmzi4yuKw346Z/3tQucpMdn97RK1WhDwnDnxJAtyYOSz
         uczw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:subject:to:references:message-id:date
         :user-agent:mime-version:in-reply-to:content-transfer-encoding
         :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=4p7IOwBs2JIRzN8FUV9+6lIz0rWPMjBRfDFCPN8usF0=;
        b=L0UzlE4Z0JdsoEgrVqaRk3B99cyZe1pQhxF4utJgTppneCYBETjyvKmoewHLlC4cst
         Rkm+/JT4WhUfhAFeiH/GL/lSgp6Tc00G3hfEz233AMkZ8/a4hubTxvwDH9ssrjiwTPfY
         NX4rssrvPeIqHpyTewSH8qTsdhcBHWcJvMHIoOQnh/GQkVrtpvQWWdFMH3DZiK4sqxuF
         koHwwSzLLnBFOnlq5f7k38ndfQntUnRX66AJK47GsktQzSZ4Y/iguSUyaBZDLcp3CEGE
         Y3BUjhO7X/P7gyayJcXuXo2cEd5NFaQeIZEImMxaQCLqQ3TM8NdI14UuYixiPCpXQh7d
  
X-Gm-Message-State: AIVw111a9TxcLQt5SlqSBhzfH1yC7oxiPXZEfDH73Xb+f2o0/GpMo/ED
	zHxFL+ovfA7GEQ==
X-Received: by 10.46.84.19 with SMTP id i19mr296290ljb.16.1500384039835;
        Tue, 18 Jul 2017 06:20:39 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.21.14 with SMTP id 14ls1691573wmv.3.canary-gmail; Tue, 18
 Jul 2017 06:20:37 -0700 (PDT)
X-Received: by 10.223.176.68 with SMTP id g4mr1256532wra.26.1500384037647;
        Tue, 18 Jul 2017 06:20:37 -0700 (PDT)
ARC-Seal: i=1; a=rsa-sha256; t=1500384037; cv=none;
        d=google.com; s=arc-20160816;
        b=oJX3lFayHX7YLyfqd1JhTNUarDLKevn8IC/VvDjdlGBxOn1TGbZmPKE+knqt1kggCG
         +sdJm/Ms6kGh2bkdW07mDIDBZYvDvJdbhSSb6SSogs8uhNnmMRALo1zZLYpQkxBtXMnJ
         +YIPyKtlb8HJT3xNyJBlIQRnrZ2NEdO5hKg0aCAcFS+OvDccsuLFfUPcJtlIDUDTrJQE
         CCIeaH0lC8NL6/+DTomLn8RgJalAUN1g5UUjtxYlcCOTP4p8dB4bpD3joUlPHM4Vv79Q
         sj1X50+1DwYsikRKItlj1Dtspt4ul5cMO9VK3h3FjxG8sOjm7yPNyXiHbHCTV7J4z5Do
         Y0sA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=content-transfer-encoding:in-reply-to:mime-version:user-agent:date
         :message-id:references:to:subject:from:arc-authentication-results;
        bh=rlz8E0/u+hgsLcgJ7FrZO4unfqC0Qc1YwksNftk7H70=;
        b=OPZ58WmpBBQuVeV56LUPmOdu3Shp6gwzAi7EApcd1YO9SbiFQWQADNBkutlkAITj8G
         lZbA9mTSy6fo0lW5mRd0Tmf+qAgFwznmUdsD0MEoXC8mrBDfTNEimM4uahXSc+imAEMu
         M1VexJS2F0brRQKviI6QOpTxZvZorOgWNZHKfqo9ujQP59n2RjiSbERmECJshOdYc4an
         XB+O6BbukWQGjceaWNeO2sFIsVGkt7RagroyPQ5b+52ALFb+C5JbRUvGu5Ii+D2VCUsa
         yb8G7/tAlKNPCUk7N1HE5iNh7qGQvDfyZkNI9M2YKSlY+F9a5A1PWxENlWXBmxGZmK2Y
         yjQQ==
ARC-Authentication-Results: i=1; mx.google.com;
       spf=pass (google.com: domain of alves.ped@gmail.com designates 209.85.128.172 as permitted sender) smtp.mailfrom=alves.ped@gmail.com
Original-Received: from mail-wr0-f172.google.com (mail-wr0-f172.google.com. [209.85.128.172])
        by mx.google.com with ESMTPS id o202si1839428wmg.30.2017.07.18.06.20.37
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 18 Jul 2017 06:20:37 -0700 (PDT)
Received-SPF: pass (google.com: domain of alves.ped@gmail.com designates 209.85.128.172 as permitted sender) client-ip=209.85.128.172;
Original-Received: by mail-wr0-f172.google.com with SMTP id a10so29511615wrd.0
        for <std-proposals@isocpp.org>; Tue, 18 Jul 2017 06:20:37 -0700 (PDT)
X-Received: by 10.223.163.197 with SMTP id m5mr1262508wrb.128.1500384036822;
        Tue, 18 Jul 2017 06:20:36 -0700 (PDT)
Original-Received: from [192.168.0.101] (bl17-148-124.dsl.telepac.pt. [188.82.148.124])
        by smtp.gmail.com with ESMTPSA id o68sm2955636wmi.21.2017.07.18.06.20.35
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 18 Jul 2017 06:20:35 -0700 (PDT)
In-Reply-To: <803a7b14-34c2-4ee6-8c5a-154532143a6e@isocpp.org>
X-Original-Sender: pedro@palves.net
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of alves.ped@gmail.com designates 209.85.128.172 as permitted sender) smtp.mailfrom=alves.ped@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:33242
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/33242>

On 07/17/2017 11:15 AM, dimeiyi24@gmail.com wrote:
> =E2=96=A1 Motivation
> In C++, the same semantic functions sometimes have to write two
> different versions, a constexpr version, an efficient general version.
> For example:
> constexpr auto StrLen(const char *s) {auto tp=3Ds;while(*s) s++;return s-=
tp;}
> auto StrLenSSE4(const char *s){...}
> =E2=96=A1 Proposal
> Gcc has a built-in function __builtin_constant_p (v) to determine
> whether a value is a compile time constant, but it can only be used for
> macros, and v can only be one literal.
> Can you provide a function of *is_constexpr*(v) to determine whether v
> is a compile time constant?
> In this way, the function above can use a unified interface.
> constexpr auto StrLen(const char *s){
>   if constexpr(*is_constexpr*(s)) {
>     auto tp=3Ds;while(*s) s++;return s-tp;
>   }
>   else return StrLenSSE4(s);
> }

Note that GCC's __builtin_constant_p _can_ be used for the
above.  It's how C++17 constexpr char_traits<>::length() etc.
is currently implemented in libstdc++ trunk.  (BTW, you can
just use char_traits<>::length() in this case.)

#include <string.h>

static constexpr bool
constant_string_p(const char* s)
{
  while (__builtin_constant_p(*s) && *s)
    s++;
  return __builtin_constant_p(*s);
}

static constexpr size_t constexpr_strlen(const char* s)
{
  auto tp =3D s;
  while (*s)
    s++;
  return s - tp;
}

constexpr size_t my_strlen(const char* s) noexcept
{
  if (constant_string_p(s))
    return constexpr_strlen(s);
  return strlen(s);
}

static_assert (my_strlen ("") =3D=3D 0);
static_assert (my_strlen ("hello") =3D=3D 5);
static_assert (my_strlen ("he\0llo") =3D=3D 2);

static const char array[] =3D "foo";
static_assert (my_strlen (array) =3D=3D 3);

constexpr bool check()
{
  char s[] =3D "str";
  s[0] =3D 'l';
  s[1] =3D '\0';
  return my_strlen(s) =3D=3D 1;
}

static_assert(check());

int main (int argc, char** argv)
{
  return my_strlen(argv[0]);
}

$ g++ constexpr_strlen.cc -o constexpr_strlen -g3 -O2 -std=3Dgnu++17=20
$ gdb --batch -q ./constexpr_strlen -ex "disassemble main"
Dump of assembler code for function main(int, char**):
   0x00000000004004c0 <+0>:     sub    $0x8,%rsp
   0x00000000004004c4 <+4>:     mov    (%rsi),%rdi
   0x00000000004004c7 <+7>:     callq  0x400490 <strlen@plt>
   0x00000000004004cc <+12>:    add    $0x8,%rsp
   0x00000000004004d0 <+16>:    retq  =20
End of assembler dump.
$

--=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/c395066b-4639-3abb-b923-63040818904c%40palves.ne=
t.

.
