Assigned `nullptr` to `bool` type. Which compiler is correct?

前端 未结 2 1152
野的像风
野的像风 2021-01-12 12:05

I have a following snippet of code that assigned nullptr to bool type.

#include 

int main()
{
    bool b = nullptr         


        
2条回答
  •  孤独总比滥情好
    2021-01-12 12:29

    This was changed by DR 1423 so that there is no implicit conversion from nullptr to bool.

    (The relevant wording changed again very recently by DR 1781 and DR 2133 but only to move the wording, not change what conversions are valid. At the time of writing, the CWG issues list doesn't show that 1781 has been resolved, but the change to the draft is at visible in git.)

    So it looks to me like Clang 3.8 implements the pre-1423 rule, and GCC 5.4 implements the post-1423 rule which doesn't allow implicit conversions from nullptr to bool.

    Current versions of Clang still allow the conversion, but give a -Wnull-conversion warning.

提交回复
热议问题