为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下

Why does this code using __LINE__ compile under MSVC in Release mode, but not in Debug mode?

本文关键字:模式 编译 MSVC 调试 LINE 代码 为什么 布模式      更新时间:2023-10-16

考虑这个程序:

#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;
}

当我尝试在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 ==========

然而,当我在Release模式下尝试时:它编译、构建、运行并产生适当的输出:

10
adds called on line 12
14

这是一个潜在的Visual Studio 2017错误吗?如果不是,为什么它在一种模式下工作而在另一种模式中不工作?

你可以在这里看到它的编译:编译器浏览器


这是调试和发布模式的命令行标志的副本:

调试

/JMC /permissive- /GS /W3 /Zc:wchar_t /Qspectre /ZI /Gm- /Od /sdl /Fd"x64Debugvc141.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"x64Debug" /EHsc /nologo /Fo"x64Debug" /Fp"x64DebugSimulator.pch" /diagnostics:classic 

发布

/permissive- /GS /GL /W3 /Gy /Zc:wchar_t /Qspectre /Zi /Gm- /O2 /sdl /Fd"x64Releasevc141.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"x64Release" /EHsc /nologo /Fo"x64Release" /Fp"x64ReleaseSimulator.pch" /diagnostics:classic 

似乎有报道:__LINE__不能用作constexpr函数的参数。

我们在C++团队中有一个已知的错误
[…]
我们已确定此问题不是错误。请参阅Jonathan的评论。

乔纳森说:

这是编译器支持"编辑并继续"的副作用(基本上,我们不希望对__LINE__值的更改被视为抑制"编辑并连续"的"粗鲁"编辑(:如果使用/Zi而不是/ZI进行编译,则代码应编译(但可执行文件不支持"编辑和继续"(
[…]
该错误被认为是一个功能。。。

来自MSVC文档:

/ZI选项类似于/Zi,但它生成的PDB文件的格式支持"编辑并继续"功能。[…]/ZI选项也与__LINE__预定义宏的使用不兼容;使用/ZI编译的代码不能将__LINE__用作非类型模板参数,尽管__LINE__可以用于宏展开。


然而,当我在发布模式下尝试时:它编译、构建、运行并产生适当的输出:

我想原因是/ZI/Zi标志的差异。您的发布模式标志具有/Zi,因此编译良好。