[英]How to get on top-level const pointer using "auto"?
In short words:简而言之:
Per C++ Primer, pg 69, "auto": "If we want the deduced type to have a top-level const , we must say so explicitly".根据 C++ Primer,第 69 页,“auto”:“如果我们希望推导类型具有顶级const ,我们必须明确说明”。
I would get an top-level const pointer:我会得到一个顶级 const 指针:
int i = 42;
const auto *p = &i;
But the resulted p has type const int *
instead of expected int * const
.但是结果 p 的类型是const int *
而不是预期的int * const
。 I can even reassign it p = 0;
我什至可以重新分配它p = 0;
. . why?为什么? (note: the format of pointer type deduction using auto *
is from the book.) (注:使用auto *
指针类型推导的格式来自书上。)
In your example, p
is a pointer to a const int
, not a const
pointer to an int
. 在您的示例中, p
是指向const int
的指针,而不是指向int
的const
指针。 The latter can be achieved with the following statement: 后者可以通过以下声明实现:
auto* const p = &i;
With auto
, you don't even need the asterisk, which makes it easy: 使用auto
,你甚至不需要星号,这很容易:
const auto p = &i;
Here, auto
deduces the type to be int *
, making it int * const
. 在这里, auto
将类型推断为int *
,使其成为int * const
。 Note that whether it's const auto
or auto const
does not make a difference, just as with a typedef
name. 请注意,无论是const auto
还是auto const
都没有区别,就像使用typedef
名称一样。
In your example, only int
fits as the deduced type, making p
a const int *
. 在您的示例中,只有int
适合作为推导类型,使p
为const int *
。 This is not a top-level const
, but a pointer to a const int
. 这不是顶级const
,而是指向const int
的指针。
Considering your original code, 考虑到您的原始代码,
int i = 42;
const auto *p = &i;
adding a 添加一个
cout << typeid(p).name() << endl;
reports 报告
int const *
with Visual C++. 用Visual C ++。
Which contradicts your statement 这与你的陈述相矛盾
” the resulted p has type
int *
“结果p的类型为int *
Here's one way to take full control: 这是一种完全控制的方法:
int i = 42;
auto const *const p = &i;
Remove the first const
if you want a const pointer to mutable object. 删除第一个const
,如果你想让一个const指针可变对象。
Alternatively, as noted by chris in his answer, if you want a const pointer to mutable object you can just do 或者,正如克里斯在他的回答中所指出的,如果你想要一个指向可变对象的const指针,你就可以做到
auto const p = &i;
The type deduction through auto
works exactly as it does for function templates. 通过auto
的类型推导与功能模板完全相同。 So, when you write const auto *p = &i;
所以,当你写const auto *p = &i;
, the type of p
is exactly the type of p
in the call to the below template which would match f(&i)
. , p
的类型恰好是对下面模板的调用中p
的类型,它与f(&i)
匹配。
template<typename U>
void f(const U* p);
Thus, the type is const int*
. 因此,类型是const int*
。 If you want p
to be int * const
, the right expression is auto * const p = &i
. 如果希望p
为int * const
,则右表达式为auto * const p = &i
。
Position of const
before/after the pointer declaration( *
) changes the meaning. Position 的const
before/after the pointer declaration( *
) 改变了意义。
Below example shows a few combination of with auto
, const
and *
- and their interpretation.下面的示例显示了auto
、 const
和*
的一些组合以及它们的解释。
int main() {
auto i = 2; // int
const auto a = &i; // int * const
// a = &j; // Error: addr is const
*a = 4; // OK (value is NOT const)
const auto* b = &i; // const int *
// *b = 4; // Error: value is const
b = &j; // OK (addr is NOT const)
auto* const c = &i; // const int * - (Your case!)
*c = 4; // OK (value is NOT const)
// c = &j; // Error: addr is const
const auto* const d = &i; // const int * const
// *d = 4; // Error: Both value & addr are const
// d = &j; // Error: Both value & addr are const
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.