simpleroseinc

Logo

Developer Tips

View Company GitHub Profile

20 February 2020

Pointers no longer initialize bool in braces

by Zhihao Yuan

In C++17 and above, the conversion from a pointer or pointer-to-member type to bool is a narrowing conversion. A braced-initialization that relies on such a conversion to happen is ill-formed.

bool a[] = {"literal"};  // ill-formed

The following example shows how does the new rule outlaw vague code in combination with designated initializers in function calls:

struct filter
{
    bool always_true;
};

void run(filter);

void f()
{
    run({.always_true = "no"});  // ill-formed
};

This change has been implemented in Clang, GCC, and MSVC1.

SimpleRose Inc sponsored this change.


1MSVC has sophisticated rules to determine whether to call out an error, to issue a warning, or to be quiet.

tags: cplusplus - initialization