user3613229
user3613229

Reputation: 101

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. Is there any purpose for using a **&&const as a function argument. The code was compiled using VC2013 with C++ Compiler Nov 2013.

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!

Upvotes: 0

Views: 109

Answers (3)

ABu
ABu

Reputation: 12269

References are alias, and alias does never change. So, &const and &&const have no sense at all. 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.

In consequence, qualifying a reference with const have no purpose at all. 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).

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.

Upvotes: 1

R Sahu
R Sahu

Reputation: 206607

Use of pointers and const can be confusing. Here is an explantion.

int i;
int * ip1 = &i;

ip1 can be changed to point another object. *ip1 can be changed to have another value.

int const* ip2 = &i;

ip2 can be changed to point another object. *ip2 can not be changed to have another value.

int * const ip3 = &i;

ip3 can not be changed to point another object. *ip3 can be changed to have another value.

int const* const ip4 = &i;

ip4 can not be changed to point another object. *ip4 can not be changed to have another value.

Now, coming to your question,

void func(int **&&const x)

is not valid code in C++. It is not valid in C++11 either.

Assuming you meant,

void func(int **&const x)

x is const reference to a pointer to a pointer to an int. That means, you can't change what x points to but you can change *x. You can also change *(*x).

Upvotes: 0

Void - Othman
Void - Othman

Reputation: 3481

gcc 4.8.2 doesn't consider it valid code:

// 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.

Upvotes: 3

Related Questions