Vio*_*ffe 35 c++ language-lawyer c++14 c++17
我很困惑.不是const auto ch = unsigned char{'p'};一个完全有效的初始化表达式?所有三个主要编译器都无法使用几乎相同的错误消息进行编译:
错误:预期'('用于函数式转换或类型构造
交换花括号('p')没有任何变化.但是,它确实在没有signedor unsigned关键字的情况下编译.
son*_*yao 42
因为只有单字类型名称可以用于这种显式类型转换.
单字类型名称后跟braced-init-list是指定类型的prvalue,使用指定的braced-init-list进行
designating a temporary (until C++17)whose result object is (since C++17)direct-list-initialized.
unsigned char不是单字类型名称,char而是.对于功能性演员表达也是如此,这也是为什么('p')不起作用的原因.
作为解决方法,你可以
using uc = unsigned char; // or use typedef
const auto ch = uc{'p'};
Run Code Online (Sandbox Code Playgroud)
或者将其更改为其他演员风格.
const auto ch = (unsigned char) 'p'; // c-style cast expression
const auto ch = static_cast<unsigned char>('p'); // static_cast conversion
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1893 次 |
| 最近记录: |