简体   繁体   English

已弃用的注册关键字C ++ 11的替换

[英]Replacement for deprecated register keyword C++ 11

我已读( 这里 ,例如),该register关键字将在C ++ 11这样弃用,是有一个相当于在标准的较新版本此存储类说明,或它由编译器照顾?

We can find the rationale for deprecating register in defect report 809: Deprecation of the register keyword which says ( emphasis mine ): 我们可以在缺陷报告809中找到弃用寄存器的基本原理:注册关键字的弃用说明( 强调我的 ):

The register keyword serves very little function, offering no more than a hint that a note says is typically ignored . register关键字提供的功能非常少,只提供一个注释表示通常被忽略的提示 It should be deprecated in this version of the standard, freeing the reserved name up for use in a future standard, much like auto has been re-used this time around for being similarly useless. 它应该在这个版本的标准中弃用,释放保留的名称以供将来的标准使用,就像这次重新使用auto一样同样没用。

The removal of register for C++17 was approved in the Lenexa meeting but it is still reserved for future use. 删除 C ++ 17 的注册表 已在Lenexa会议上获得批准,但仍保留供将来使用。

The register keyword was deprecated in the 2011 C++ standard, as its effect was already implicit in the language . register关键字在2011 C ++标准中已弃用, 因为其效果已隐含在该语言中 It remains reserved for future use by the standard, and is time to remove its vestigial specification. 它仍然保留供标准未来使用,是时候删除它的退化规范。

Because of the as-if rule the compiler only has to emulate the observable behavior of the program and therefore the optimizer can via the as-if rule choose to keep a variable in a register if it won't effect observable behavior and presumably will in most cases make better choices since it usually has more information. 由于as-if规则 ,编译器只需要模拟程序的可观察行为 ,因此优化器可以通过as-if规则选择将变量保留在寄存器中,如果它不会影响可观察的行为,并且可能会在大多数情况下做出更好的选择,因为它通常有更多信息

For reference also see role of "register" C keyword? 另请参阅“注册”C关键字的作用? from the gcc mailing list, one of the replies in the thread says: gcc邮件列表中,该帖子中的一个回复说:

I don't think the "register" keyword ever affected register allocation in gcc. 我认为“register”关键字不会影响gcc中的寄存器分配。 For that you have to go back to compilers of the 1970s. 为此你必须回到20世纪70年代的编译器。

The register keyword does still have a use, though, in a gcc extension: gcc uses it in combination with asm to implement register variables. 但是,在gcc扩展中,register关键字仍然有用:gcc与asm结合使用它来实现寄存器变量。

It was never a guarantee that the compiler would listen to you if you used the keyword (and in some cases, it was a guarantee it would ignore you, like if you took the address). 如果你使用了关键字,那么编译器就不会保证会听你的(在某些情况下,它会保证它会忽略你,就像你拿了地址一样)。

This hinting ability is now deprecated, and there is no replacement (that is standard. inline asm is not standard, but could be used). 此提示功能现已弃用,并且没有替换(这是标准的.inline asm不是标准的,但可以使用)。

假设编译器可以比程序员更好地将变量分配给寄存器,因此不推荐使用register ,新标准中没有其他等效关键字。

Just a note that using JetBrains ReShaper in Visual Studio 2015 , I was told register keyword is deprecated in C++11 and removed in C++17 and suggested me to replace it with auto keyword for this piece of code: 只是注意到在Visual Studio 2015中使用JetBrains ReShaper ,我被告知在C++11不推荐使用register关键字并在C++17删除它,并建议我用这段代码的auto关键字替换它:

float constant = 1.0f / 478.01;
int centerX = (pointcloud.width >> 1);
int centerY = (pointcloud.height >> 1);
auto depth_idx = 0;
for (auto v = -centerY; v < centerY; ++v)
{
    for (auto u = -centerX; u < centerX; ++u, ++depth_idx)
    {
        pcl::PointXYZ& pt = pointcloud.points[depth_idx];
        pt.z = depth_data[depth_idx] * 0.001f;
        pt.x = static_cast<float> (u) * pt.z * constant;
        pt.y = static_cast<float> (v) * pt.z * constant;
    }
}

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

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