Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: 'auto' and const references

Reply
Thread Tools

Re: 'auto' and const references

 
 
Balog Pal
Guest
Posts: n/a
 
      05-04-2013
On 5/3/2013 3:16 PM, Juha Nieminen wrote:
> Let's say that a function returns a const reference. If I say this:
>
> auto x = thatFunction();
>
> what would the type of 'x' be? Will it be a const reference, thus
> eliding copying, or will a copy be made?


IIRC auto will pick up type stripped of many things. If your function
returns T& or const T & or const t, auto will still be T. But you can
write auto& or better yet auto const& that works for most cases.

> Is there any difference to these:
>
> auto& x = thatFunction();
>
> const auto& x = thatFunction();


Due to reference collapsing these will be the same if const T& was
retuned. But different to plain auto.

You can use decltype() instead of auto to preserve the reference, and in
C++14 decltype(auto) will be usable in some contexts.


 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      05-04-2013
Am 04.05.2013 16:33, schrieb Balog Pal:
> On 5/3/2013 3:16 PM, Juha Nieminen wrote:
>> Let's say that a function returns a const reference. If I say this:
>>
>> auto x = thatFunction();
>>
>> what would the type of 'x' be? Will it be a const reference, thus
>> eliding copying, or will a copy be made?

>
> IIRC auto will pick up type stripped of many things. If your function
> returns T& or const T & or const t, auto will still be T. But you can
> write auto& or better yet auto const& that works for most cases.
>
>> Is there any difference to these:
>>
>> auto& x = thatFunction();
>>
>> const auto& x = thatFunction();

>
> Due to reference collapsing these will be the same if const T& was
> retuned.


I don't think reference collapsing has anything to do with it. These are
just plain template argument deduction rules.

> You can use decltype() instead of auto to preserve the reference, and in
> C++14 decltype(auto) will be usable in some contexts.


Looks like I have to catch up on what's going on w.r.t. standardization.

Cheers!
SG
 
Reply With Quote
 
 
 
 
Balog Pal
Guest
Posts: n/a
 
      05-05-2013
On 5/4/2013 7:47 PM, SG wrote:
>> You can use decltype() instead of auto to preserve the reference, and in
>> C++14 decltype(auto) will be usable in some contexts.

>
> Looks like I have to catch up on what's going on w.r.t. standardization.


Several proposals were about using auto in more places. One such place
is return of a function, any function, without trailing ->. Like for
current lambda, deducing the type from the actual returns in the source.
Simple auto would follow the usual auto rules an you can instead use
decltype(auto) to use the decltype rules and preserve lvalueness.

The post-Bristol mailings are not posted yet, but I guess they will be
in two weeks or so.

 
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
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
const references -- how to document intent of const qualification Andrew R. Thomas-Cramer Java 8 07-11-2003 05:01 PM



Advertisments