为什么使用__LINE__的此代码在MSVC下以发布模式而不是在调试模式下进行编译? [英] Why does this code using __LINE__ compile under MSVC in Release mode, but not in Debug mode?

查看:120
本文介绍了为什么使用__LINE__的此代码在MSVC下以发布模式而不是在调试模式下进行编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑此程序:

#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生成这些编译器错误:

When I try to compile and build this in Debug mode Visual Studio 2017 is generating these compiler errors:

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 模式下尝试此操作时:它会编译,生成,运行并产生适当的输出:

However, when I try this under Release mode: It compiles, builds, runs, and produces the appropriate output:

10
adds called on line 12
14

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

Is this a potential Visual Studio 2017 bug? If not, why does it work in one mode and not the other?

您可以在此处查看其编译方式:编译器浏览器

You can see it compiled here: Compiler Explorer

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

Here's a copy of the command line flags for both debug and release modes:

调试

/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 


推荐答案

似乎被报道为:< a href = https://developercommunity.visualstudio.com/content/problem/195665/-line-cannot-be-used-as-an-argument-for-constexpr.html rel = noreferrer> __ LINE __ 不能用作constexpr函数的参数。

Seems like it was reported: __LINE__ cannot be used as an argument for constexpr functions.


我们有一个已知的错误

[...]

我们确定此问题不是错误。请参考乔纳森的评论。

We have a known bug for this issue on the C++ team here.
[...]
We have determined that this issue is not a bug. Please refer to comments of Jonathan.

乔纳森说:


这是编译器支持编辑并继续的副作用(基本上,我们不希望将 __ LINE __ 的值更改为粗鲁编辑,取消编辑并继续):如果您使用 / Zi 而不是 / ZI 进行编译,则代码应该编译(但可执行文件不支持编辑并继续。)

[...]

该错误被认为是一个功能.. 。

This is a side-effect of the compilers support for Edit-and-Continue (basically we don't want a change to the value of __LINE__ to be considered a 'rude' edit that suppresses Edit-and-Continue): if you compiler with /Zi instead of /ZI then the code should compile (but the executable won't support Edit-and-Continue).
[...]
The bug is considered a feature ...

来自 MSVC文档


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

The /ZI option is similar to /Zi, but it produces a PDB file in a format that supports the Edit and Continue feature. [...] The /ZI option is also incompatible with use of the __LINE__ predefined macro; code compiled with /ZI can't use __LINE__ as a non-type template argument, although __LINE__ can be used in macro expansions.






但是,当我在发布模式下尝试此操作时:它会编译,生成,运行并产生适当的输出:

However, when I try this under Release mode: It compiles, builds, runs, and produces the appropriate output:

我猜它的原因是 / ZI / Zi 标志的区别。您的发布模式标志具有 / Zi ,因此可以正常编译。

I guess the reason for it is the /ZI vs /Zi flag difference. Your release mode flags have /Zi so it compiles fine.

这篇关于为什么使用__LINE__的此代码在MSVC下以发布模式而不是在调试模式下进行编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆