mardi 3 mars 2020

nullptr to int conversation, who is right?

Consider such code:

#include <cstdint>

using my_time_t = uintptr_t;

int main()
{
    const my_time_t t = my_time_t(nullptr);
}

It failed to compile with msvc v19.24:

<source>(7): error C2440: '<function-style-cast>': cannot convert from 'nullptr' to 'my_time_t'

<source>(7): note: A native nullptr can only be converted to bool or, using reinterpret_cast, to an integral type

<source>(7): error C2789: 't': an object of const-qualified type must be initialized

<source>(7): note: see declaration of 't'

Compiler returned: 2

but clang (9.0.1) and gcc (9.2.1) "eat" this code without any errors.

I like msvc behaviour, but is it confirmed by standard? In other words is it bug in clang or gcc, or it is possible to interprent standard that this is right behaviour from gcc/clang?

Aucun commentaire:

Enregistrer un commentaire