Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #70077 > unrolled thread

A question about if constexpr

Started byDaniel <danielaparker@gmail.com>
First post2019-11-27 05:45 -0800
Last post2019-12-01 14:42 +0100
Articles 9 on this page of 49 — 14 participants

Back to article view | Back to comp.lang.c++


Contents

  A question about if constexpr Daniel <danielaparker@gmail.com> - 2019-11-27 05:45 -0800
    Re: A question about if constexpr "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-11-27 15:03 +0100
      Re: A question about if constexpr Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-11-29 16:33 +0000
        Re: A question about if constexpr Öö Tiib <ootiib@hot.ee> - 2019-11-29 16:52 -0800
          Re: A question about if constexpr David Brown <david.brown@hesbynett.no> - 2019-11-30 12:03 +0100
            Re: A question about if constexpr Öö Tiib <ootiib@hot.ee> - 2019-11-30 03:47 -0800
              Re: A question about if constexpr David Brown <david.brown@hesbynett.no> - 2019-11-30 15:08 +0100
    Re: A question about if constexpr Sam <sam@email-scan.com> - 2019-11-27 18:40 -0500
      Re: A question about if constexpr Öö Tiib <ootiib@hot.ee> - 2019-11-27 23:16 -0800
        Re: A question about if constexpr Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-11-29 16:35 +0000
          Re: A question about if constexpr Öö Tiib <ootiib@hot.ee> - 2019-11-29 16:41 -0800
      Re: A question about if constexpr David Brown <david.brown@hesbynett.no> - 2019-11-28 10:24 +0100
        Re: A question about if constexpr Bo Persson <bo@bo-persson.se> - 2019-11-28 11:58 +0100
          Re: A question about if constexpr David Brown <david.brown@hesbynett.no> - 2019-11-28 12:37 +0100
          Re: A question about if constexpr Robert Wessel <robertwessel2@yahoo.com> - 2019-11-28 14:04 -0600
            Re: A question about if constexpr David Brown <david.brown@hesbynett.no> - 2019-11-29 09:23 +0100
              Re: A question about if constexpr Robert Wessel <robertwessel2@yahoo.com> - 2019-11-29 14:42 -0600
        Re: A question about if constexpr Daniel <danielaparker@gmail.com> - 2019-11-28 21:09 -0800
          Re: A question about if constexpr "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-11-29 07:41 +0100
          Re: A question about if constexpr Öö Tiib <ootiib@hot.ee> - 2019-11-28 23:50 -0800
            Re: A question about if constexpr David Brown <david.brown@hesbynett.no> - 2019-11-29 09:38 +0100
              Re: A question about if constexpr Öö Tiib <ootiib@hot.ee> - 2019-11-29 17:15 -0800
                Re: A question about if constexpr Paavo Helde <myfirstname@osa.pri.ee> - 2019-11-30 08:19 +0200
                  Re: A question about if constexpr Ned Latham <nedlatham@woden.valhalla.oz> - 2019-11-30 00:24 -0600
                    Re: A question about if constexpr Öö Tiib <ootiib@hot.ee> - 2019-11-30 02:13 -0800
                      Re: A question about if constexpr Ned Latham <nedlatham@woden.valhalla.oz> - 2019-11-30 04:56 -0600
                        Re: A question about if constexpr Öö Tiib <ootiib@hot.ee> - 2019-11-30 03:20 -0800
                          Re: A question about if constexpr Ned Latham <nedlatham@woden.valhalla.oz> - 2019-11-30 05:38 -0600
                            Re: A question about if constexpr Öö Tiib <ootiib@hot.ee> - 2019-11-30 04:57 -0800
                              Re: A question about if constexpr Ned Latham <nedlatham@woden.valhalla.oz> - 2019-11-30 09:46 -0600
                                Re: A question about if constexpr Öö Tiib <ootiib@hot.ee> - 2019-11-30 11:09 -0800
                                  Re: A question about if constexpr Ned Latham <nedlatham@woden.valhalla.oz> - 2019-11-30 20:03 -0600
                                    Re: A question about if constexpr Öö Tiib <ootiib@hot.ee> - 2019-11-30 23:43 -0800
                                      Re: A question about if constexpr Ned Latham <nedlatham@woden.valhalla.oz> - 2019-12-01 04:56 -0600
                                        Re: A question about if constexpr Christian Gollwitzer <auriocus@gmx.de> - 2019-12-01 18:33 +0100
                                          Re: A question about if constexpr Ned Latham <nedlatham@woden.valhalla.oz> - 2019-12-01 19:27 -0600
                                            Re: A question about if constexpr boltar@nowhere.co.uk - 2019-12-02 08:34 +0000
                                              Re: A question about if constexpr Ned Latham <nedlatham@woden.valhalla.oz> - 2019-12-02 04:21 -0600
                                          Re: A question about if constexpr boltar@nowhere.co.uk - 2019-12-02 08:33 +0000
                                          Re: A question about if constexpr Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-12-03 17:05 +0000
                                            Re: A question about if constexpr boltar@nowhere.co.uk - 2019-12-03 17:09 +0000
                                              Re: A question about if constexpr Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-12-03 18:03 +0000
                                                Re: A question about if constexpr boltar@nowhere.co.uk - 2019-12-04 16:49 +0000
                                                  Re: A question about if constexpr Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-12-04 18:36 +0000
                                                    Re: A question about if constexpr boltar@nowhere.co.uk - 2019-12-05 12:51 +0000
          Re: A question about if constexpr David Brown <david.brown@hesbynett.no> - 2019-11-29 09:30 +0100
        Re: A question about if constexpr Paavo Helde <myfirstname@osa.pri.ee> - 2019-11-29 10:51 +0200
    Re: A question about if constexpr Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-28 13:52 -0800
    Re: A question about if constexpr Bonita Montero <Bonita.Montero@gmail.com> - 2019-12-01 14:42 +0100

Page 3 of 3 — ← Prev page 1 2 [3]


#70216

Fromboltar@nowhere.co.uk
Date2019-12-03 17:09 +0000
Message-ID<qs64sd$3f4$1@gioia.aioe.org>
In reply to#70215
On Tue, 3 Dec 2019 17:05:03 +0000
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote:
>On 01/12/2019 17:33, Christian Gollwitzer wrote:
>> Am 01.12.19 um 11:56 schrieb Ned Latham:
>>> XBNF is no mere scripting language; it is a mathematical meta-language
>>> capable of defining the syntax of any 
>> 
>>> context-free grammar: 
>> here is your problem: C is not context free. It is usually dealt with the
>"lexer hack" to use a parser for context-free grammar.
>
>neos can compile code for languages which aren't definable using a context
>free grammar; neos also deals with BOTH the syntax AND the semantics of a
>language.
>
>> Also, parsing the syntax is only a minor step in most actual compilers. The
>big pile of work is in the optimizer (which is in theory optional) and the
>code generator, and another big pile is in the library. Pure C without the
>library is good for nothing.
>> 
>> That's why most regulars in this group are rather sceptical of Flibble's
>project. A more realistic approach would sit on top of LLVM, where years of
>work have been done already.
>
>Try speaking for yourself and not for others unless you own some kind of
>fucking crystal ball.

I'll speak for me - I'm sure your project is fun and important for you, but no 
one else gives a rats arse.

[toc] | [prev] | [next] | [standalone]


#70217

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2019-12-03 18:03 +0000
Message-ID<YrxFF.19$uy5.2@fx10.dcg>
In reply to#70216
On 03/12/2019 17:09, boltar@nowhere.co.uk wrote:
> On Tue, 3 Dec 2019 17:05:03 +0000
> Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote:
>> On 01/12/2019 17:33, Christian Gollwitzer wrote:
>>> Am 01.12.19 um 11:56 schrieb Ned Latham:
>>>> XBNF is no mere scripting language; it is a mathematical meta-language
>>>> capable of defining the syntax of any
>>>
>>>> context-free grammar:
>>> here is your problem: C is not context free. It is usually dealt with the
>> "lexer hack" to use a parser for context-free grammar.
>>
>> neos can compile code for languages which aren't definable using a context
>> free grammar; neos also deals with BOTH the syntax AND the semantics of a
>> language.
>>
>>> Also, parsing the syntax is only a minor step in most actual compilers. The
>> big pile of work is in the optimizer (which is in theory optional) and the
>> code generator, and another big pile is in the library. Pure C without the
>> library is good for nothing.
>>>
>>> That's why most regulars in this group are rather sceptical of Flibble's
>> project. A more realistic approach would sit on top of LLVM, where years of
>> work have been done already.
>>
>> Try speaking for yourself and not for others unless you own some kind of
>> fucking crystal ball.
> 
> I'll speak for me - I'm sure your project is fun and important for you, but no
> one else gives a rats arse.

You just contradicted yourself, dear.

/Flibble

-- 
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin

“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who doesn’t believe in any God the most. Oh, no..wait.. that never happens.” – Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."

[toc] | [prev] | [next] | [standalone]


#70244

Fromboltar@nowhere.co.uk
Date2019-12-04 16:49 +0000
Message-ID<qs8o2v$6jt$1@gioia.aioe.org>
In reply to#70217
On Tue, 3 Dec 2019 18:03:41 +0000
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote:
>On 03/12/2019 17:09, boltar@nowhere.co.uk wrote:
>> On Tue, 3 Dec 2019 17:05:03 +0000
>> Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote:
>>> On 01/12/2019 17:33, Christian Gollwitzer wrote:
>>>> Am 01.12.19 um 11:56 schrieb Ned Latham:
>>>>> XBNF is no mere scripting language; it is a mathematical meta-language
>>>>> capable of defining the syntax of any
>>>>
>>>>> context-free grammar:
>>>> here is your problem: C is not context free. It is usually dealt with the
>>> "lexer hack" to use a parser for context-free grammar.
>>>
>>> neos can compile code for languages which aren't definable using a context
>>> free grammar; neos also deals with BOTH the syntax AND the semantics of a
>>> language.
>>>
>>>> Also, parsing the syntax is only a minor step in most actual compilers. The
>
>>> big pile of work is in the optimizer (which is in theory optional) and the
>>> code generator, and another big pile is in the library. Pure C without the
>>> library is good for nothing.
>>>>
>>>> That's why most regulars in this group are rather sceptical of Flibble's
>>> project. A more realistic approach would sit on top of LLVM, where years of
>>> work have been done already.
>>>
>>> Try speaking for yourself and not for others unless you own some kind of
>>> fucking crystal ball.
>> 
>> I'll speak for me - I'm sure your project is fun and important for you, but
>no
>> one else gives a rats arse.
>
>You just contradicted yourself, dear.

No, its my accurate opinion that no one else gives a damn.

[toc] | [prev] | [next] | [standalone]


#70252

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2019-12-04 18:36 +0000
Message-ID<o0TFF.3619$Tt1.1116@fx06.dcg>
In reply to#70244
On 04/12/2019 16:49, boltar@nowhere.co.uk wrote:
> On Tue, 3 Dec 2019 18:03:41 +0000
> Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote:
>> On 03/12/2019 17:09, boltar@nowhere.co.uk wrote:
>>> On Tue, 3 Dec 2019 17:05:03 +0000
>>> Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote:
>>>> On 01/12/2019 17:33, Christian Gollwitzer wrote:
>>>>> Am 01.12.19 um 11:56 schrieb Ned Latham:
>>>>>> XBNF is no mere scripting language; it is a mathematical meta-language
>>>>>> capable of defining the syntax of any
>>>>>
>>>>>> context-free grammar:
>>>>> here is your problem: C is not context free. It is usually dealt with the
>>>> "lexer hack" to use a parser for context-free grammar.
>>>>
>>>> neos can compile code for languages which aren't definable using a context
>>>> free grammar; neos also deals with BOTH the syntax AND the semantics of a
>>>> language.
>>>>
>>>>> Also, parsing the syntax is only a minor step in most actual compilers. The
>>
>>>> big pile of work is in the optimizer (which is in theory optional) and the
>>>> code generator, and another big pile is in the library. Pure C without the
>>>> library is good for nothing.
>>>>>
>>>>> That's why most regulars in this group are rather sceptical of Flibble's
>>>> project. A more realistic approach would sit on top of LLVM, where years of
>>>> work have been done already.
>>>>
>>>> Try speaking for yourself and not for others unless you own some kind of
>>>> fucking crystal ball.
>>>
>>> I'll speak for me - I'm sure your project is fun and important for you, but
>> no
>>> one else gives a rats arse.
>>
>> You just contradicted yourself, dear.
> 
> No, its my accurate opinion that no one else gives a damn.

That is not just speaking for yourself, dear.

/Flibble

-- 
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin

“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who doesn’t believe in any God the most. Oh, no..wait.. that never happens.” – Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."

[toc] | [prev] | [next] | [standalone]


#70280

Fromboltar@nowhere.co.uk
Date2019-12-05 12:51 +0000
Message-ID<qsaugv$fg1$1@gioia.aioe.org>
In reply to#70252
On Wed, 4 Dec 2019 18:36:05 +0000
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote:
>On 04/12/2019 16:49, boltar@nowhere.co.uk wrote:
>> On Tue, 3 Dec 2019 18:03:41 +0000
>> Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote:
>>> On 03/12/2019 17:09, boltar@nowhere.co.uk wrote:
>>>> On Tue, 3 Dec 2019 17:05:03 +0000
>>>> Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote:
>>>>> On 01/12/2019 17:33, Christian Gollwitzer wrote:
>>>>>> Am 01.12.19 um 11:56 schrieb Ned Latham:
>>>>>>> XBNF is no mere scripting language; it is a mathematical meta-language
>>>>>>> capable of defining the syntax of any
>>>>>>
>>>>>>> context-free grammar:
>>>>>> here is your problem: C is not context free. It is usually dealt with the
>
>>>>> "lexer hack" to use a parser for context-free grammar.
>>>>>
>>>>> neos can compile code for languages which aren't definable using a context
>
>>>>> free grammar; neos also deals with BOTH the syntax AND the semantics of a
>>>>> language.
>>>>>
>>>>>> Also, parsing the syntax is only a minor step in most actual compilers.
>The
>>>
>>>>> big pile of work is in the optimizer (which is in theory optional) and the
>
>>>>> code generator, and another big pile is in the library. Pure C without the
>
>>>>> library is good for nothing.
>>>>>>
>>>>>> That's why most regulars in this group are rather sceptical of Flibble's
>>>>> project. A more realistic approach would sit on top of LLVM, where years
>of
>>>>> work have been done already.
>>>>>
>>>>> Try speaking for yourself and not for others unless you own some kind of
>>>>> fucking crystal ball.
>>>>
>>>> I'll speak for me - I'm sure your project is fun and important for you, but
>
>>> no
>>>> one else gives a rats arse.
>>>
>>> You just contradicted yourself, dear.
>> 
>> No, its my accurate opinion that no one else gives a damn.
>
>That is not just speaking for yourself, dear.

Never mind. Btw, you need to work on your patronisation skills.

[toc] | [prev] | [next] | [standalone]


#70142

FromDavid Brown <david.brown@hesbynett.no>
Date2019-11-29 09:30 +0100
Message-ID<qrqkv8$ba1$1@dont-email.me>
In reply to#70105
On 29/11/2019 06:09, Daniel wrote:
> On Thursday, November 28, 2019 at 4:25:03 AM UTC-5, David Brown wrote:
>>
>> If the compiler is not
>> generating identical code for an "if constexpr" and an "if" where the
>> expression happens to be known at compile time (whether it is
>> technically a C++ constant or not), then the compiler is doing a poor
>> job at optimising.
>>
> Which raises the question, what is the point of the constexpr if statement anyway? 
> 
> Daniel
> 

The part omitted by the false branch has to be able to compile, but
otherwise doesn't affect the code.  For example:

const bool return_int = true;

auto foo(void) {
	if constexpr(return_int) {
		return 1;
	} else {
		return 1.0;
	}
}

The return type of "foo" is determined by the "if constexpr".  Without
the "constexpr", you would get an inconsistent deduction for the return
type.

Typically, you will see "if constexpr" in templates, I expect.

[toc] | [prev] | [next] | [standalone]


#70144

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2019-11-29 10:51 +0200
Message-ID<qrqm5v$h19$1@dont-email.me>
In reply to#70081
On 28.11.2019 11:24, David Brown wrote:
> I had a check at <https://godbolt.org> with the simpler test variation:
>
>
> #include <string>
>
> int foo()
> {
>      constexpr int n = 1;
>      constexpr int m = 10;
>      std::string s = "abcd";
>
>      if (n < m || s.empty())
>      {
>          return 1;
>      }
>      return 0;
> }
>
> It turns out that MSVC (/O2 /Wall /std:c++17) generates a whole pile of
> muck for that code.  It takes about 40 lines of assembly (excluding
> string library functions) while it creates and deletes the string, adds
> calls to "__security_check_cookie", whatever that is.  However, it
> generates the same pile when "constexpr" is added.  (It also generates
> an extraordinary quantity of warnings from its own headers here - is
> that normal for MSVC, or is it an artefact of the godbolt.org setup or
> my incomplete knowledge of MSVC command line parameters?)
>
> gcc and clang, on the other hand generates a single "return 1;" result
> whether you have "constexpr" or not.  And no warnings with "-Wall -Wextra".

An interesting example demonstrating the benefits of short string 
optimization. If the constructor or destructor of an otherwise 
effectively unused local object (as s above) call non-inlined functions, 
the optimizer cannot (so easily) remove the whole instance. Indeed, the 
following code produces ca 30 lines of assembly with gcc 9.2 and 
-std=c++17 -O3, including calls to std::basic_string::_M_create and 
operator delete:

#include <string>

int foo()
{
     constexpr int n = 1;
     constexpr int m = 10;
     std::string s = "abcdabcdabcdabcd";

     if (n < m || s.empty())
     {
         return 1;
     }
     return 0;
}

MSVC also has short string optimization, but it seems by some reason the 
optimizer has not understood it can apply that for std::string s = 
"abcd"; or std::string s("abcd"). Interestingly enough, if one writes 
the string initialization as

    std::string s("abcd", 4);

then also MSVC is able to optimize it away and produces:

int foo(void) PROC                                        ; foo
         mov     eax, 1
         ret     0
int foo(void) ENDP                                        ; foo


So it appears this example is more about SSO and does not tell much 
about constexpr.

[toc] | [prev] | [next] | [standalone]


#70096

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2019-11-28 13:52 -0800
Message-ID<86d0dbact8.fsf@linuxsc.com>
In reply to#70077
Daniel <danielaparker@gmail.com> writes:

> Consider
>
> #include <string>
> #include <iostream>
>
> int main()
> {
>     constexpr int n = 1;
>     constexpr int m = 10;
>     std::string s = "abcd";
>
>     if (n < m || s.empty())
>     {
>         std::cout << "ok\n";
>     }
> }
>
> Is it reasonable that vs2019 produces a warning
>
> warning C4127:  conditional expression is constant
> consider using 'if constexpr' statement instead

Let me rephrase your question as a multiple choice:

   "A compiler operating under default option settings
    produces this warning message for the above program.
    Would you be more likely to describe this choice as

       (A) reasonable, or

       (B) monumentally stupid?"

Asked like this, my answer would be (B).

There are, I am sure, a large number of related questions,
for which I expect I would give various answers depending
on the particular question.

[toc] | [prev] | [next] | [standalone]


#70188

FromBonita Montero <Bonita.Montero@gmail.com>
Date2019-12-01 14:42 +0100
Message-ID<qs0g03$2i1$1@news.albasani.net>
In reply to#70077
I think a warning here is stupid because any compiler will optimize away
the comparison anyway. A "if constexpr ( )" is just for aesthetics here.

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.c++


csiph-web