Why doesn’t “auto ch = unsigned char{‘p’}” compile under C++ 17?

Because only single-word type name could be used for this kind of explicit type conversion.

A single-word type name followed by a braced-init-list is a prvalue of the specified type designating a temporary (until C++17) whose result object is (since C++17) direct-list-initialized with the specified braced-init-list.

unsigned char is not a single-word type name, while char is. And this is true for functional cast expression too, that’s why ('p') doesn’t work either.

As the workaround, you can

using uc = unsigned char;  // or use typedef
const auto ch = uc{'p'};

Or change it to other cast styles.

const auto ch = (unsigned char) 'p';  // c-style cast expression
const auto ch = static_cast<unsigned char>('p');  // static_cast conversion

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)