samedi 28 octobre 2017

copy initialization of bool from nullptr

Consider following program:

#include <iostream>
int main() {
    bool s=nullptr;
    std::cout<<std::boolalpha;
    std::cout<<s;
}

g++ 7.2.0 gives following error. See live demo here

prog.cc: In function 'int main()':
prog.cc:3:9: error: converting to 'bool' from 'std::nullptr_t' requires direct-initialization [-fpermissive]
  bool s=nullptr;
         ^~~~~~~

clang++ 5.0.0 compiles this program & gives following warning & gives false as an output. See live demo here

prog.cc:3:9: warning: implicit conversion of nullptr constant to 'bool' [-Wnull-conversion]
        bool s=nullptr;
             ~ ^~~~~~~
               false
1 warning generated.

This program also compiles successfully on Microsoft visual C++ compiler. I've also used /Za option to disable compiler extensions. See live demo copy initialization of bool from nullptr

Intel C++ compiler also compiles this program successfully. See live demo here

So, the question is which compiler is right here ? Is there any defect report regarding this ? As far as I understand g++ is right here to not accept this code. But I would like to know what the C++ standard says about this ? Is this a major bug in popular C++ compilers except g++ ?

Aucun commentaire:

Enregistrer un commentaire