4

I do understand that const T*& is a reference of pointer to const type T. The pointer has low-level const so that it won't change the value it points to. However, the following code fails at compile time and gives the following message:

error C2664: 'void pointer_swap(const int *&,const int *&)': cannot convert argument 1 from 'int *' to 'const int *&'.

Is there any way to modify the pointer but prevent the pointed to value from changing in the function?

void pointer_swap(const int *&pi, const int *&pj)
{
    const int *ptemp = pi;
    pi = pj;
    pj = ptemp;
}

int main()                                                                
{                                    
    int i = 1, j = 2;                
    int *pi = &i, *pj = &j;          
    pointer_swap(pi, pj);
    return 0;
}
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • 1
    You have an `int*` and require a `const int*` as input. so changing pi and pj to `const int*` fixes the error. I am not sure why there is no implicit conversion from non-const to const though. – Hayt Sep 06 '16 at 09:57
  • 1
    @Hayt - Because of the reference. It would allow the function to do something like `pi = &something_that_really_is_const;`, which would then allow the caller to modify `something_that_really_is_const`. – Oliver Charlesworth Sep 06 '16 at 09:58

3 Answers3

4

You can't do this because you can't bind a reference-to-const to a reference-to-non-const.*

You could roll your own, but it makes more sense to just use std::swap, which is designed explicitly for this purpose, and fully generic:

#include <algorithm>

std::swap(pi, pj);

[Live example]


* Because that would allow things like this:

int       *p = something_non_const();
const int *q = something_really_const();
const int *&r = p;
r = q;     // Makes p == q
*p = ...;  // Uh-oh

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

making pi and pj to const in main function.

#include <iostream>
using namespace std;

void pointer_swap(const int *&pi, const int *&pj)
{
    const int *ptemp = pi;
    pi = pj;
    pj = ptemp;
}

int main()                                                                
{                                    
    int i = 1, j = 2;                
    const int *pi = &i, *pj = &j;          
    pointer_swap(pi, pj);
    return 0;
}
Undefined Behaviour
  • 729
  • 2
  • 8
  • 27
0

this is my think. wish to help you.

void fun1(const int * p) 
{
}

int * pa = 0;
fun1(pa); //there is implicit conversion from int * to const int *

void fun2(const int & p)
{

}
int a = 0;
fun2(a); //there is implicit conversion from int & to const int &.

both examples indicate that the compiler will help us to make the conversion from current-type to const current-type. because us tell the compiler the param is const.

now, look this:

void fun3(const int * &p) 
{
//this declaration only states that i want non-const &, it's type is const int * .
}

int *pi = 0;
fun3(pi); // error C2664

The implicit conversion from non-const to const of you wish is not occur,because the function declaration only states that i want to non-const &, it's type is const int * .

Summer
  • 209
  • 2
  • 9