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


Groups > comp.lang.c++ > #70082

Re: A question about if constexpr

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Bo Persson <bo@bo-persson.se>
Newsgroups comp.lang.c++
Subject Re: A question about if constexpr
Date Thu, 28 Nov 2019 11:58:40 +0100
Lines 101
Message-ID <h49nj1Fa201U1@mid.individual.net> (permalink)
References <9d930f21-6639-49a3-8ef2-5de724f18255@googlegroups.com> <cone.1574898034.956666.129095.1004@monster.email-scan.com> <qro3p4$gd7$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
X-Trace individual.net 6ZaalrPaF22v25X93xn8Tw7LM/hWz/dcq7B+CWrsHnPMiSyBPV
Cancel-Lock sha1:93h1neh+M5fv0SeSHXG5AbTROjk=
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.1
In-Reply-To <qro3p4$gd7$1@dont-email.me>
Content-Language sv
Xref csiph.com comp.lang.c++:70082

Show key headers only | View raw


On 2019-11-28 at 10:24, David Brown wrote:
> On 28/11/2019 00:40, Sam wrote:
>> Daniel 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
>>
>> The compiler does not reject this code as ill-formed; and only issues a
>> non-fatal diagnostic. This is entirely the compiler's prerogative.
>>
>> Whether this or something else is "reasonable" is often a somewhat
>> subjective matter. Someone might find it reasonable, this is a suggested
>> optimization.
> 
> It should not be a "suggested optimisation".  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.
> 
> 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".
> 
> 

You should probably blaim this on the settings for Godbolt.org

Running locally, and adding a main function containing return foo(), it 
results in

--- C:\Users\BoP\source\reference\quick_test.cpp 
-------------------------------
    return foo();
00007FF6BB091000  mov         eax,1
}
00007FF6BB091005  ret


On the other hand, Visual Studio reports that it is using the compiler 
options

//permissive- /GS- /Zc:rvalueCast /GL /W4 /Gy /Zc:wchar_t /Zi /Gm- /O2 
/Ob2 /Fd"x64\Release\vc142.pdb" /Zc:inline /fp:precise /D "_UNICODE" /D 
"UNICODE" /errorReport:prompt /GF /WX /Zc:forScope /GR /arch:AVX2 /Gd 
/Oy /Oi /MD /std:c++latest /FC /Fa"x64\Release\" /EHsc /nologo 
/Fo"x64\Release\" /Ot /Fp"x64\Release\quick_test_ms.pch" 
/diagnostics:column


Don't know exactly which one makes the difference.  :-)


    Bo Persson

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web