Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Why can't the source compile? (http://www.velocityreviews.com/forums/t456390-why-cant-the-source-compile.html)

sword 08-23-2006 07:40 AM

Why can't the source compile?
 
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 *& '


Phlip 08-23-2006 07:46 AM

Re: Why can't the source compile?
 
sword wrote:

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

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!



sword 08-23-2006 07:59 AM

Re: Why can't the source compile?
 
But if I write the function like this:

void test(int* a);


It will be OK.


Mark P 08-23-2006 08:11 AM

Re: Why can't the source compile?
 
Phlip wrote:
> sword wrote:
>
>> 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

Ian Collins 08-23-2006 08:13 AM

Re: Why can't the source compile?
 
sword wrote:
> 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.

--
Ian Collins.

Isold.Wang@gmail.com 08-23-2006 08:23 AM

Re: Why can't the source compile?
 
void test(int* a);


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


sword wrote:
> 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 *& '



Greg Comeau 08-23-2006 12:42 PM

Re: Why can't the source compile?
 
In article <p4UGg.12315$kO3.8289@newssvr12.news.prodigy.com >,
Mark P <usenet@fall2005REMOVE.fastmailCAPS.fm> wrote:
>Phlip wrote:
>> sword wrote:
>>
>>> 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.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

Ron Natalie 08-23-2006 01:02 PM

Re: Why can't the source compile?
 
Mark P wrote:

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

Frederick Gotham 08-23-2006 03:51 PM

Re: Why can't the source compile?
 
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.

--

Frederick Gotham


All times are GMT. The time now is 08:17 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.