Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #70149
| Subject | Re: A question about if constexpr |
|---|---|
| Newsgroups | comp.lang.c++ |
| References | <9d930f21-6639-49a3-8ef2-5de724f18255@googlegroups.com> <qrlvnd$f18$1@dont-email.me> |
| From | Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> |
| Organization | Jupiter Mining Corporation |
| Message-ID | <yLbEF.24511$l54.23391@fx12.dcg> (permalink) |
| Date | 2019-11-29 16:33 +0000 |
On 27/11/2019 14:03, Alf P. Steinbach wrote:
> On 27.11.2019 14:45, Daniel wrote:
>> 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
>>
>> g++ and clang do not, even with -Wall or -pedantic
>>
>> The context is generated code where n = 1, ... 10, 11 ...
>
> It's reasonable here because `n < m` is always true, which is known at compile time, and `||` has short-circuit evaluation, which means that the non-`constexpr` empty checking of the string is not considered.
>
> If you switch the values of `n` and `m` then `if constexpr` can no longer be used, because then, though the result is also then knowable at compile time, it isn't /necessarily/ known at compile time. The compiler needs some smarts to understand that `s.empty()` must be `false` here.
>
> That said, I find the Visual C++ warning about constant conditions to be generally unreasonable. For example, it pops up if you write `while(2+2 == 4)` instead of the slightly more idiomatic `for(;;)`. Apparently it no longer does that for `while(true)`, but I just don't trust it, it's evidently tuned to ignore one special literal case, and that's it.
VC++ is correct to warn about while(2+2 == 4) and not for(;;) the later being the only acceptable why to do an endless loop (I don't agree that while(true) is just as good as it is syntactically less clean). The reason why VC++ is correct to warn about while(2+2 == 4) is that one of those 2's could be a typo and a variable was meant instead.
/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."
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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