【发布时间】:2019-08-22 09:30:37
【问题描述】:
a 遇到的具体问题是编译器处理它的方式存在一些不一致。
例如此代码(https://godbolt.org/z/08Z-zi):
constexpr auto value = 1;
static_assert(*const_cast<int *>(&value), "value should be 1");
使用 GCC、Clang 和 MSVC 编译良好,但使用 Intel C++ Compiler 19.0.1 编译失败,并出现以下错误:
error: expression must have a constant value
static_assert(*const_cast<int *>(&value), "value should be 1");
据我所知,该标准并未明确声明常量表达式中不允许使用 const_cast。通过结果指针写入将是未定义的,因此不允许,但读取应该没问题。
考虑到所有主要编译器都编译此代码(包括 ICC
【问题讨论】:
标签: c++ language-lawyer constexpr const-cast constant-expression