【发布时间】:2019-01-31 02:44:54
【问题描述】:
我目前正在使用 constexpr 数组,我注意到我无法使用 /std:c++17 或 /std:c++ 在 MSVC 19.15.26726 下编译以下(有效)代码最新:
#include <array>
using array_type = std::array<unsigned int, 3>;
using iterator_type = array_type::const_iterator;
constexpr array_type arr{ { 1,2,3 } };
constexpr iterator_type getIteratorBefore(iterator_type it) {
return std::prev(it);
}
constexpr iterator_type test = getIteratorBefore(arr.end());
忽略我从 IntelliSense 获得的所有突出显示错误和显示 std::array 的错误模棱两可(似乎与同一文件中的一些奇怪的 array() 函数冲突),我在最后一行:
error C4146: unary minus operator applied to unsigned type, result still unsigned
error C4308: negative integral constant converted to unsigned type
warning C4307: '+': integral constant overflow
它在 gcc (x86-64 gcc (trunk)) 和 MSVC (x86-64 编辑: MSVC Pre 2018 with /std:c++17 works) 下的编译器资源管理器中编译良好(没有测试其他)。
我真的没有想法。当我把它放在 main 方法中时,相同的代码会编译,所以这似乎是 constexpr 范围的问题。
【问题讨论】:
-
看起来像 MSVC 中的一个简单错误。应该可以在 C++17 模式下编译。
-
C4146 和 C4308 对我来说是相同版本的警告
-
@SergeyA 嗯,您似乎使用了错误的编译器标志(请参阅最后的输出语句)。对于 MSVC,它是 /std:c++17。不过没关系,因为我在帖子中提到的版本不知道参数(?)。它仍然认为我们在 c++11 中,不允许非文字类型作为 constexpr 函数返回类型。
-
我使用
/std:c++latest编译,代码编译,test指向 3。您显示的两个错误对我来说是警告,但代码确实编译。 -
@JulianWiesler 我指的是代码,而不是编译器。贴出的代码是有效的C++17代码,如果MSVC编译失败,就是MSVC的bug。
标签: c++ visual-c++ compiler-errors c++17 constexpr