Why can't the source compile?

S

sword

void test(int*& a);


int main()
{
int a[4];
test(a);
return 0;
}



Who can tell me the reason why the source above can't compile?
I use VC++ 6.0.
test.cpp(7) : error C2664: 'test' : cannot convert parameter 1 from
'int [4]' to 'int *& '
 
P

Phlip

sword said:
void test(int*& a);

a is a reference to a pointer. That means you must pass a pointer, for the
reference to seat on.
int a[4];
test(a);

a is not a pointer, it's an array.

Either take out the & (most likely what you need), or introduce a pointer:

int * p = a;
test(p);

And you might want to read ahead in your tutorial, to soak this stuff in...
 
M

Mark P

Phlip said:
sword said:
void test(int*& a);

a is a reference to a pointer. That means you must pass a pointer, for the
reference to seat on.
int a[4];
test(a);

a is not a pointer, it's an array.

But it can decay to a pointer. The issue is that when it's viewed as a
pointer, it's a constant pointer (i.e., it must point to the same place
always). And you can't pass "T const" as "T&" since the constant
qualifier is discarded someone may attempt to modify the T&.

Rewrite the declaration of test as:

void test( int* const& a );

and it should compile.


-Mark
 
I

Ian Collins

sword said:
void test(int*& a);


int main()
{
int a[4];
test(a);
return 0;
}



Who can tell me the reason why the source above can't compile?

You can pass an array of int to a function with int* as its parameter,
but there isn't an automatic conversion from an array of int to an int
pointer reference.
 
G

Greg Comeau

Phlip said:
sword said:
void test(int*& a);

a is a reference to a pointer. That means you must pass a pointer, for the
reference to seat on.
int a[4];
test(a);

a is not a pointer, it's an array.

But it can decay to a pointer. The issue is that when it's viewed as a
pointer, it's a constant pointer (i.e., it must point to the same place
always). And you can't pass "T const" as "T&" since the constant
qualifier is discarded someone may attempt to modify the T&.

Rewrite the declaration of test as:

void test( int* const& a );

and it should compile.

This code looks right, and brings the elaboration full circle.
I would suggest though that the OP avoid this because unless
I'm missing something obvious there does not seem to be anything
gained over int * in this case.
 
R

Ron Natalie

Mark said:
But it can decay to a pointer. The issue is that when it's viewed as a
pointer, it's a constant pointer (i.e., it must point to the same place
always).

No it's not. When it is a pointer, it is an rvalue that has been
converted form the array. It's type is NOT const (although it's
not possible to legally modify it).
And you can't pass "T const" as "T&" since the constant
qualifier is discarded someone may attempt to modify the T&.

There's no "const" qualifier here to discard. The issue is that
you can not bind non-const references to rvalues regardless of
the constness of the rvalue.
Rewrite the declaration of test as:

void test( int* const& a );

and it should compile.
This is correct.
 
F

Frederick Gotham

sword posted:
void test(int*& a);


int main()
{
int a[4];
test(a);
return 0;
}


An array can decay to a pointer to its first element -- however, the pointer
it yields is an R-value. A reference to non-const cannot bind to an R-value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top