【发布时间】:2020-06-15 19:57:36
【问题描述】:
考虑这个程序:
#include <cstdint>
using my_time_t = uintptr_t;
int main() {
const my_time_t t = my_time_t(nullptr);
}
使用 msvc v19.24 编译失败:
<source>(5): error C2440: '<function-style-cast>': cannot convert from 'nullptr' to 'my_time_t'
<source>(5): note: A native nullptr can only be converted to bool or, using reinterpret_cast, to an integral type
<source>(5): error C2789: 't': an object of const-qualified type must be initialized
<source>(5): note: see declaration of 't'
Compiler returned: 2
但是 clang (9.0.1) 和 gcc (9.2.1) “吃掉”这段代码没有任何错误。
我喜欢 MSVC 的行为,但它是否符合标准? 换句话说,它是 clang/gcc 中的错误还是可以解释 标准,这是来自 gcc/clang 的正确行为?
【问题讨论】:
-
我将其解读为来自函数样式转换的复制初始化。然后由编译器as one of the C++ casts 解释,“即使它无法编译”。也许编译器之间在如何解释强制转换方面存在不一致
-
据我所知 MSVC v19.24 不支持 C++11 模式。您是指 C++14 还是 C++17?
标签: c++ c++11 gcc visual-c++ clang