简体   繁体   English

在指向指针右值const函数参数的指针中使用“ const”会做什么?

[英]What does 'const' do when used in a pointer to pointer rvalue const function argument?

void func(int **&&const x) 
{   
    *(*x) = 32; 

}

void main()
{
    int *pi = new int{ 64 };

    printf("x : %d\n", *pi);

    func(&pi);

    printf("x : %d\n", *pi);
}


Outputs: 
x : 64 
x : 32 

When using a pointer to a pointer to an rvalue const, the value is still modifiable within the function. 当使用指向右值const的指针时,该值仍可在函数中修改。 Is there any purpose for using a **&&const as a function argument. 使用** && const作为函数参数有什么目的? The code was compiled using VC2013 with C++ Compiler Nov 2013. 该代码是使用VC2013和C ++ Compiler于2013年11月编译的。

EDIT: I do receive the warning "anachronism used : qualifiers on reference are ignored" but it's probably better to fail to compile completely. 编辑:我确实收到警告“使用过时:引用上的限定符将被忽略”,但最好不要完全编译。 Thanks for the answers! 感谢您的回答!

gcc 4.8.2 doesn't consider it valid code: gcc 4.8.2认为它不是有效的代码:

// snippet of your code
void func(int **&& const x)
{
    *(*x) = 32;

}

... and the compile ... ...然后编译...

$ g++ -fsyntax-only -Wall -pedantic -std=c++11 foo.cpp
foo.cpp:2:26: error: ‘const’ qualifiers cannot be applied to ‘int**&&’
 void func(int **&& const x)
                          ^

I'm going to assume that VC 2013 is wrong to allow that code to compile. 我将假设VC 2013在允许编译该代码方面是错误的。

References are alias, and alias does never change. 引用是别名,别名永远不会改变。 So, &const and &&const have no sense at all. 因此, &const&&const完全没有意义。 They are semantically equivalent in all matters to & and && . 在语义上与&&&等价。

So is the case that that constructions aren't allowed (perhaps for simplifying metaprogramming). 不允许这种构造的情况也是这样(也许是为了简化元编程)。 Not only for int**&&const , but also for int& const or int&& const . 不仅适用于int**&&const ,而且适用于int& constint&& const

In consequence, qualifying a reference with const have no purpose at all. 因此,使用const限定引用完全没有任何目的。 Your funcion can be rewritted then to: 您可以将功能改写为:

void func(int **&& x) 
{   
    *(*x) = 32; 
}

The next token is a rvalue-reference. 下一个标记是右值引用。 Its purpose is detecting if the receiving argument is anonymous or not. 其目的是检测接收参数是否为匿名。 For example: 例如:

int **ppi = π

func(ppi);

doesn't work, ppi is not an anonymous variable (it's a name), but &pi it is (it's only an address, so, a pure rvalue ). 不起作用, ppi不是一个匿名变量(它是一个名称),但&pi它是(它只是一个地址,因此是pure rvalue )。

It's important to remark that, inside func , x is a lvalue-reference, and not a rvalue-reference , since inside the function block, x is no more an anonymous variable (its name is just x ), irrespective of the "anonymousity" of its origin. 重要的是要指出,在funcx是lvalue-reference而不是rvalue-reference ,因为在功能块内部, x不再是匿名变量(其名称仅为x ),而与“ anonymousity”无关其起源。

Use of pointers and const can be confusing. 指针和const可能会造成混淆。 Here is an explantion. 这是外植体。

int i;
int * ip1 = &i;

ip1 can be changed to point another object. 可以将ip1更改为指向另一个对象。 *ip1 can be changed to have another value. *ip1可以更改为另一个值。

int const* ip2 = &i;

ip2 can be changed to point another object. 可以将ip2更改为指向另一个对象。 *ip2 can not be changed to have another value. *ip2不能更改为另一个值。

int * const ip3 = &i;

ip3 can not be changed to point another object. ip3不能更改为指向另一个对象。 *ip3 can be changed to have another value. *ip3可以更改为另一个值。

int const* const ip4 = &i;

ip4 can not be changed to point another object. ip4不能更改为指向另一个对象。 *ip4 can not be changed to have another value. *ip4不能更改为另一个值。

Now, coming to your question, 现在,提您的问题,

void func(int **&&const x)

is not valid code in C++ . C++不是有效的代码。 It is not valid in C++11 either. C++11也无效。

Assuming you meant, 假设你的意思是

void func(int **&const x)

x is const reference to a pointer to a pointer to an int. x是指向指向int的指针的const引用。 That means, you can't change what x points to but you can change *x . 这意味着,您不能更改x指向的内容,但可以更改*x You can also change *(*x) . 您还可以更改*(*x)

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

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