为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下
Why does this code using __LINE__ compile under MSVC in Release mode, but not in Debug mode?
考虑这个程序:
#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
,因此编译良好。
相关文章:
- 为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下
- 在发布模式下启动使用库的静态链接编译的应用程序时出错
- 使用Qt Creator在调试模式下编译一段代码
- 在调试模式下编译时qrc_resource.cpp错误
- CMake:如何在Visual Studio环境中将依赖于模式的编译标志传递给nvcc
- 在Visual Studio 2017上无法使用调试模式x64进行OpenCV3.2编译
- 为什么这些 CRTP 模式中只有一个可以编译?
- Visual Studio 是否有具有多个下拉框的编译模式?
- C++文件可配置的编译时多态模式?
- X86系统的编译读取线(静态模式)
- 在 C++17 之前的模式下无法使用静态 constexpr 进行编译
- 痘痘设计模式会阻止您编译吗?
- lower_bound()函数不能在调试模式下编译
- 自定义分配器仅在VS 2015中的发布模式下编译
- 一般来说,使用Qt Creator,是什么导致程序在调试模式下编译时正确运行,但在发布模式下崩溃
- 为什么这个奇怪的重复出现的模板模式示例不编译?
- 无法在Win32发布模式下编译QT应用程序
- 生成文件中的多种编译模式
- 以"simple c++"模式编译的程序在从QtCreator启动时不起作用
- 以c模式编译c++代码