CV - qualifiers of an auto variable

风流意气都作罢 提交于 2020-07-09 08:08:11

问题


I found here the following "rule":

[...] auto drops const and volatile qualifiers only if they're at the top or right below an outermost reference [...]

I understand that top-level cv-qualifiers are a description of the variable itself (compared to the description of what it is pointing to or referencing). But when is a cv-qualifier "right below an outermost reference" and why would auto drop it (probably the first question answers the second one as well)?


回答1:


"cv right below an outermost reference" means that the reference is to a cv-qualified type. For example, take this function:

const int& foo();

The type "right below the outermost reference" is const int, which means the const is there as well. In this code:

auto i = foo();

the type of i is int, not const int or const int&.

Examples of a constwhich is not right below an outermost reference are:

const char* bar();
const double* volatile & baz();

Using auto to take calls of these functions would deduce to type const char* and const double*, respectively.



来源:https://stackoverflow.com/questions/31492967/cv-qualifiers-of-an-auto-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!