C++ Logo

std-proposals

Advanced search

Re: Prevent Assignments from contextually converting to bool

From: Nicholl, Ryan <Ryan.Nicholl_at_[hidden]>
Date: Thu, 24 Oct 2019 19:40:48 +0000
I have to strongly oppose the suggested change. It breaks old working code just to make it harder to make beginner mistakes. C++ is expert oriented, we shouldn’t be making work for ourselves just to make the language slightly cushier.

From: Std-Proposals <std-proposals-bounces_at_lists.isocpp.org> On Behalf Of Brian Bi via Std-Proposals
Sent: Wednesday, October 23, 2019 12:44 PM
To: std-proposals_at_[hidden]
Cc: Brian Bi <bbi5291_at_[hidden]>
Subject: Re: [std-proposals] Prevent Assignments from contextually converting to bool

*External Message* - Use caution before opening links or attachments

On Wed, Oct 23, 2019 at 11:35 AM connor horman via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>> wrote:
This is my proposed to solution to one of the worst logic errors in any number of languages, being:
if(i=0)
vs.
if(i==0)

The proposal is to tweak the wording of contextual conversions to bool, prohibiting unparenthesised assignments from being eligible for contextual conversion. Code that intends to do this can simply put parenthesis around the assignment or use an init-statement, and code that does this without intention (i.e. intends the comparison) would be elevated from a logic error (the worst kind honestly) to a compile-time error.
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]cpp.org<mailto:Std-Proposals_at_[hidden]>
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals<https://urldefense.com/v3/__https:/lists.isocpp.org/mailman/listinfo.cgi/std-proposals__;!9cTk8lEYrHI!9qEH-MtCUe6TcGGK6ll0wRqzY4HrdNbgYNjlM15ICBh_7WSj9Jj26V7ecf7H10A$>

You can achieve this effect using compiler warnings. For example, with GCC and Clang, you can use the flag -Werror=parentheses to treat this as an error.

I do not think we should add more rules to the language that discriminate among expressions on the basis of properties other than their type and value category.

--
Brian Bi

Received on 2019-10-24 14:43:05