考虑以下程序:
#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"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
释放
/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
好像有人报告过:__LINE__
不能用作constexpr函数的参数。
我们的C ++小组在这里有一个已知的bug。
[...]
我们已确定此问题不是错误。请参考乔纳森的评论。
乔纳森说:
这是编译器支持“编辑并继续”的副作用(基本上,我们不希望将值的更改
__LINE__
视为抑制“编辑并继续”的“粗略”编辑):如果使用/Zi
而不是/ZI
然后应编译代码(但可执行文件将不支持“编辑并继续”)。
[...]
该错误被认为是功能...
从MSVC文档:
该
/ZI
选项与相似/Zi
,但是它以支持“编辑并继续”功能的格式生成PDB文件。[...]该/ZI
选项还与__LINE__
预定义宏的使用不兼容;尽管可以在宏扩展中使用,但/ZI
使用编译的代码不能__LINE__
用作非类型模板参数__LINE__
。
但是,当我在“发布”模式下尝试此操作时:它会编译,生成,运行并产生适当的输出:
我猜这是因为/ZI
vs /Zi
标志的不同。您的释放模式标志具有,/Zi
因此可以正常编译。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句