Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > returning a reference

Reply
Thread Tools

returning a reference

 
 
bob smith
Guest
Posts: n/a
 
      09-04-2013
Can someone help me understand what really happens when a function returns a reference?


For instance, there is this function:

http://www.cplusplus.com/reference/v...ector/operator[]/


Is it really returning a pointer?

Thanks.
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      09-04-2013
bob smith <(E-Mail Removed)> writes:
>Can someone help me understand what really happens when a function returns a reference?


A reference - as far as I understand it - is not a run-time
value, but a certain kind of source code name or type.

When a name or type is tagged to be a reference, this
modifies the semantics of certain definitions and expressions.

So, it indeed modifies also the run-time behavior, but
this does not mean that references exist as run-time entites.

>For instance, there is this function:
>http://www.cplusplus.com/reference/v...ector/operator[]/
>Is it really returning a pointer?


It might be a help to rewrite code that uses references to
code using pointers as an intermediate step in understanding
references. But a true understanding of references is
reached only when one can describe their semantics without
using pointers.

Personally, I see references as means to introduce names
(or aliases) for already-existing objects. So, I'd say that
a reference return means that you grant the caller the
right to bind a name to the refered object. (You see that
I do not use the word »pointer« here.)

 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      09-05-2013
On Wednesday, 4 September 2013 23:37:27 UTC+3, bob smith wrote:
> Can someone help me understand what really happens when a function returns
> a reference?


With reference we usually mean a thing that is very similar to immutable
pointers that must point at actual object. If reference refers to temporary
then that increases life-time of such temporary until end of life time of
reference. The main benefit above pointers are those constraints and that
effect.

> For instance, there is this function:
>
> http://www.cplusplus.com/reference/v...ector/operator[]/
>
> Is it really returning a pointer?


No.

All usages of operator[] in example in that article are returning
'std::vector<int,std::allocator<int>>::reference' that must be typedefed as
'std::allocator<int>::reference' that must be typedefed as
'int&' if I understand standard correctly. That 'int&' is ordinary reference
to int.

In general 'std::vector<A,B> operator[](size_type)' must return
'B::reference' and 'std::vector<A,B> operator[](size_type) const' must
return 'B::const_reference' whatever it is. It can be pretty much anything.
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      09-05-2013
On Thu, 05 Sep 2013 02:54:06 -0700, Öö Tiib wrote:

> In general 'std::vector<A,B> operator[](size_type)' must return
> 'B::reference' and 'std::vector<A,B> operator[](size_type) const' must
> return 'B::const_reference' whatever it is. It can be pretty much anything.


C++11 requires that "std::vector<T,Alloc>::reference" is "T&" (similarly
for the const version). Prior versions only required that it was
"Alloc::reference", which needn't be an actual reference.

Also, std::vector<bool,Alloc> is specialised, and the reference types
typically won't be actual references but proxies which "look like"
references insofar as they provide suitable "operator =" and
"operator bool" methods. This allows the vector to store each element as a
single bit, which precludes the use of actual references.

 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      09-06-2013
On Thursday, 5 September 2013 20:11:15 UTC+3, Nobody wrote:
> On Thu, 05 Sep 2013 02:54:06 -0700, Öö Tiib wrote:
>
> > In general 'std::vector<A,B> operator[](size_type)' must return
> > 'B::reference' and 'std::vector<A,B> operator[](size_type) const' must
> > return 'B::const_reference' whatever it is. It can be pretty much anything.

>
> C++11 requires that "std::vector<T,Alloc>::reference" is "T&" (similarly
> for the const version). Prior versions only required that it was
> "Alloc::reference", which needn't be an actual reference.
>
> Also, std::vector<bool,Alloc> is specialised, and the reference types
> typically won't be actual references but proxies which "look like"
> references insofar as they provide suitable "operator =" and
> "operator bool" methods. This allows the vector to store each element as a
> single bit, which precludes the use of actual references.


Thanks for clarifying! That was unexpected for me. Technically C++11
breaks its own rules if 'std::vector<bool>::reference' is not
'bool&' but 'std::vector<T,X>::reference' (IOW that 'X::reference') MUST
be 'T&'. In previous version that 'vector<bool>' was more useful as it
was at least possible to construct similar family of vectors by using
clever allocators.
 
Reply With Quote
 
Urs Thuermann
Guest
Posts: n/a
 
      09-17-2013
Öö Tiib <(E-Mail Removed)> writes:

> With reference we usually mean a thing that is very similar to immutable
> pointers that must point at actual object. If reference refers to temporary
> then that increases life-time of such temporary until end of life time of
> reference. The main benefit above pointers are those constraints and that
> effect.


I don't think references increase the life-time of the object they
refer to. It's the programmers job to ensure the reference isn't used
after the object it refers to has disappeared. E.g., the following
has undefined behavior and you may get a compiler warning:

class A { int a; };
const A &foo() {
return A();
}

Surprisingly, GCC doesn't see the undefined behavior if you pass the
reference to the temporary object through another function:

class A { int a; };
const A &bar(const A &a) {
return a;
}
const A &foo() {
return bar(A());
}

urs
 
Reply With Quote
 
Martin Shobe
Guest
Posts: n/a
 
      09-17-2013
On 9/17/2013 12:01 PM, Urs Thuermann wrote:
> Öö Tiib <(E-Mail Removed)> writes:
>
>> With reference we usually mean a thing that is very similar to immutable
>> pointers that must point at actual object. If reference refers to temporary
>> then that increases life-time of such temporary until end of life time of
>> reference. The main benefit above pointers are those constraints and that
>> effect.

>
> I don't think references increase the life-time of the object they
> refer to. It's the programmers job to ensure the reference isn't used
> after the object it refers to has disappeared. E.g., the following
> has undefined behavior and you may get a compiler warning:
>
> class A { int a; };
> const A &foo() {
> return A();
> }


Actually, they can. They just don't in that situation. However, the
following code is ok.

#include <iostream>

int foo()
{
return 42;
}

int main()
{
int const & bar = foo();

// Binding the temporary to bar extended it's lifetime.
// See 12.2.5 in n3242.
std::cout << bar << std::endl;

return 0;
}

[snip]

Martin Shobe

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      09-17-2013
Martin Shobe <(E-Mail Removed)> writes:
>// Binding the temporary to bar extended it's lifetime.
>// See 12.2.5 in n3242.


See also

http://herbsutter.com/2008/01/01/got...portant-const/

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-18-2013
On Wednesday, 18 September 2013 12:15:46 UTC+1, Juha Nieminen wrote:
> Martin Shobe <(E-Mail Removed)> wrote:


> Basically, temporaries created inside a scope will live for as long as
> any reference inside that same scope refers to them, but no longer.


That's false. The lifetime of a temporary will only be extended
to that of the reference it is used to initialize directly.
Other references to the temporary do not extend its lifetime,
e.g.:

int const&
ref( int const& ri )
{
return ri;
}

//
int const& ri = ref( 42 );

will result in a dangling reference.

--
James
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
Returning a reference to an object PGR Java 6 01-04-2006 12:08 AM
Returning array: reference or deep copy? Ken Java 3 06-01-2004 03:43 PM
Returning reference to an array Andrew Fleet Perl 1 10-26-2003 11:52 AM
Returning a reference to an existing C++ object as a reference JustMe Perl Misc 1 08-29-2003 07:02 AM



Advertisments