简体   繁体   English

__LINE__ 不是 MSVC 中的 constexpr

[英]__LINE__ is not constexpr in MSVC

ok, the latest VS 2019 Community, local "all defaults" C++ console project:好的,最新的 VS 2019 社区,本地“全部默认”C++ 控制台项目:

int main()
{
    // cl Version 19.21.27702.2 for x86
    // 
    constexpr auto MSCVER = _MSC_VER; // 1921
    constexpr auto MSCFULLVER = _MSC_FULL_VER; //192127702
    constexpr auto MSCBUILD = _MSC_BUILD; // 2
    /*
    : error C2131:  expression did not evaluate to a constant
    : message :  failure was caused by non-constant arguments or reference to a non-constant symbol
    : message :  see usage of '__LINE__Var'
    */
    constexpr auto LINE = __LINE__;
}

But.但。 Seemingly the same compiler on Godbolt compiles this ok. Godbolt 上似乎是同一个编译器可以编译这个。 As ever before.一如既往。

https://godbolt.org/z/rn44rN https://godbolt.org/z/rn44rN

Any idea?任何的想法?

The Status as of 2019-07-22截至 2019-07-22 的状态

Apparently, this is a bug which is a feature.显然,这是一个错误,它是一个功能。 Ugh.啊。 And there is a macro which is almost perfect, besides the fact it casts to int, and the type of the LINE is long.并且有一个几乎完美的宏,除了它转换为 int 之外,而且LINE的类型很长。 Here is my version:这是我的版本:

#define _DBJ_CONCATENATE_(a, b) a ## b
#define _DBJ_CONCATENATE(a, b)  _DBJ_CONCATENATE_(a, b)

#define CONSTEXPR_LINE long(_DBJ_CONCATENATE(__LINE__,U)) 

Original is here .原文在这里 I had almost the same solution, but I was adding a zero instead of U. Perhaps because I spent hours trying to figure out what is going on.我有几乎相同的解决方案,但我添加了一个零而不是 U。也许是因为我花了几个小时试图弄清楚发生了什么。

I am sorry, but the reasoning of the MSVC team on that page is just strange.我很抱歉,但是 MSVC 团队在该页面上的推理很奇怪。 I am wondering is there a detail in the standard which resolves this issue.我想知道标准中有没有解决这个问题的细节。

Many thanks to commenters for pointing me in the right direction.非常感谢评论者指出我正确的方向。 But there is this remaining mistery: How come Godbolt + MSVC, has no problems with this same code?但是还有一个谜团:Godbolt + MSVC 怎么用同样的代码没有问题?

Apparently, this is already a known issue by the MSVC community:显然,这已经是MSVC 社区的一个已知问题

We have a known bug for this issue on the C++ team here.我们在 C++ 团队这里有一个关于这个问题的已知错误。 The status on this Developer Community item will be updated as that bug is looked at.此开发人员社区项目的状态将在查看该错误时更新。 Thanks again for reporting this to us.再次感谢您向我们报告此事。

Apparently, it is considered a "feature" of the compiler:显然,它被认为是编译器的一个“特性”:

The bug is considered a feature: Edit-and-Continue has a small but vocal and enthusiastic group of users (mostly game developers)该错误被认为是一项功能:编辑并继续拥有一小部分但有声望且热情的用户(主要是游戏开发人员)

A user in the linked thread offers the following as a workaround:链接线程中的用户提供以下解决方法:

 #define CAT(X,Y) CAT2(X,Y) #define CAT2(X,Y) X##Y #define USABLE_LINE int(CAT(__LINE__,U)) //appending 'U' shouldn't change much as __LINE__ is supposedly non-negative anyway

The MSVC team points out in that link that if you change the "Debug Information Format" from "/ZI" (Program Database for Edit and Continue) to "/Zi" (Program Database) this also fixes it (but disables edit and continue). MSVC 团队在该链接中指出,如果您将“调试信息格式”从“/ZI”(用于编辑和继续的程序数据库)更改为“/ Zi”(程序数据库),这也会修复它(但禁用编辑并继续)。 It worked for me.它对我有用。

If you're not using Edit and Continue this seems like the right fix.如果您不使用“编辑并继续”,这似乎是正确的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM