Fra*_*ler 19 c++ compiler-errors visual-c++ compiler-bug visual-studio-2017
考虑这个程序:
#include <iostream>
template<bool Debug = false, int Line = __LINE__>
constexpr int adds(const int& a, const int& b) {
if (Debug)
std::cout << __FUNCTION__ << " called on line " << Line << '\n';
return (a + b);
}
int main() {
std::cout << adds(3, 7) << '\n';
std::cout << adds<true, __LINE__> (5, 9) << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在Debug
Visual Studio 2017 模式下编译和构建它时,会生成这些编译器错误:
1>------ Build started: Project: Simulator, Configuration: Debug x64 ------
1>main2.cpp
1>c:\***\main2.cpp(12): error C2672: 'adds': no matching overloaded function found
1>c:\***\main2.cpp(12): error C2975: 'Line': invalid template argument for 'adds', expected compile-time constant expression
1>c:\***\main2.cpp(3): note: see declaration of 'Line'
1>Done building project "Simulator.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Run Code Online (Sandbox Code Playgroud)
但是,当我在Release
mode:下尝试此操作时:它编译、构建、运行并生成适当的输出:
10
adds called on line 12
14
Run Code Online (Sandbox Code Playgroud)
这是一个潜在的 Visual Studio 2017 错误吗?如果不是,为什么它在一种模式下工作而不是在另一种模式下工作?
你可以在这里看到它的编译:Compiler Explorer
这是调试和发布模式的命令行标志的副本:
调试
/JMC /permissive- /GS /W3 /Zc:wchar_t /Qspectre /ZI /Gm- /Od /sdl /Fd"x64\Debug\vc141.pdb" /Zc:inline /fp:precise /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++latest /FC /Fa"x64\Debug\" /EHsc /nologo /Fo"x64\Debug\" /Fp"x64\Debug\Simulator.pch" /diagnostics:classic
Run Code Online (Sandbox Code Playgroud)
释放
/permissive- /GS /GL /W3 /Gy /Zc:wchar_t /Qspectre /Zi /Gm- /O2 /sdl /Fd"x64\Release\vc141.pdb" /Zc:inline /fp:precise /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oi /MD /std:c++latest /FC /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Fp"x64\Release\Simulator.pch" /diagnostics:classic
Run Code Online (Sandbox Code Playgroud)
Waq*_*med 22
似乎有人报道:__LINE__
不能用作 constexpr 函数的参数。
我们在 C++ 团队这里有一个关于这个问题的已知错误。
[...]
我们已确定此问题不是错误。请参考乔纳森的评论。
乔纳森说:
这是编译器支持 Edit-and-Continue 的副作用(基本上我们不希望更改 的值
__LINE__
被视为抑制 Edit-and-Continue 的“粗鲁”编辑):如果您编译/Zi
而不是/ZI
代码应该编译(但可执行文件不支持编辑并继续)。
[...]
该错误被认为是一项功能...
来自MSVC 文档:
该
/ZI
选项类似于/Zi
,但它以支持“编辑并继续”功能的格式生成 PDB 文件。[...] 该/ZI
选项也与使用__LINE__
预定义宏不兼容;编译的代码/ZI
不能__LINE__
用作非类型模板参数,但__LINE__
可以在宏扩展中使用。
但是,当我在 Release 模式下尝试此操作时:它会编译、构建、运行并生成适当的输出:
我猜它的原因是/ZI
vs/Zi
标志的差异。您的发布模式标志具有,/Zi
因此可以正常编译。