Re: 'auto' and const references

Discussion in 'C++' started by Victor Bazarov, May 3, 2013.

  1. On 5/3/2013 9:16 AM, 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?


    I think the type should be the same as of the expression. If the
    expression gives you the const ref, then 'x' will be a const ref and
    will bind to the value returned by the function. Say, 'thatFunction' is
    declared

    const double& thatFunction();

    then the type of 'x' is going to be const double&.

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


    Only if 'thatFunction' does not return a const ref.

    The declaration 'auto' works essentially like a template type deduction.
    The compiler needs to solve the "equation"

    decltype(x) == decltype(thatFunction());

    in which it will find out the "missing" parts that 'auto' will supply
    when the declaration of 'x' is complete.

    So, for instance, if 'thatFunction()' is like before,

    decltype(x) == const double&

    which gives you

    const auto& == const double&

    , you can drop the 'const', you can drop the '&', so you get that the
    'auto' supplies 'double'. IOW, the declaration of 'x' will be the same as

    const double& x

    Now, take your example with 'auto& x = ', and you get

    auto & x = const double &

    you can drop the '&' (since it's the most inner specifier), so you get
    'auto' to equal 'const double'. That doesn't change the fact that 'x'
    is going to be a 'const double&', however.

    Catch my drift?

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 3, 2013
    #1
    1. Advertising

  2. Victor Bazarov

    SG Guest

    Am 03.05.2013 15:33, schrieb Victor Bazarov:
    > On 5/3/2013 9:16 AM, 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?

    >
    > I think the type should be the same as of the expression. If the
    > expression gives you the const ref, then 'x' will be a const ref and
    > will bind to the value returned by the function.


    The _type_ of an _expression_ is _never_ a reference type. The declared
    return type of a function is different from the type of the invocation
    expression. The return type does however affects the type as well as the
    value category of the expression. Examples:

    declared | invocation expression's
    return type | type | value category
    -------------+-----------+------------------
    int | int | prvalue
    int& | int | lvalue
    int&& | int | xvalue
    const int& | const int | lvalue

    etc.

    And no, x won't be a reference unless you write auto& x, auto&& x, const
    auto& x, etc...

    > declared
    >
    > const double& thatFunction();
    >
    > then the type of 'x' is going to be const double&.


    No, it would be just double.

    > The declaration 'auto' works essentially like a template type deduction.
    > The compiler needs to solve the "equation"
    >
    > decltype(x) == decltype(thatFunction());


    This is wrong.

    > in which it will find out the "missing" parts that 'auto' will supply
    > when the declaration of 'x' is complete.


    You apparently don't know how template argument deduction and/or
    decltype works.

    Cheers!
    SG
    SG, May 3, 2013
    #2
    1. Advertising

  3. Victor Bazarov

    SG Guest

    Am 03.05.2013 17:12, schrieb Leigh Johnston:
    > On 03/05/2013 14:33, Victor Bazarov wrote:
    >> On 5/3/2013 9:16 AM, 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?

    >>
    >> I think the type should be the same as of the expression. If the
    >> expression gives you the const ref, then 'x' will be a const ref and
    >> will bind to the value returned by the function. Say, 'thatFunction' is
    >> declared

    >
    > If you only "think" you know the answer to the question rather than
    > actually *knowing* the answer to the question you probably shouldn't
    > answer the question (on Usenet); it just wastes time.


    Well, he had the courtosy of admitting that he's not sure about his
    answer. That's better than pretending to know the answer. :)

    And Victor, I'm sorry if my wording came across too harsh.
    All is good.

    Cheers!
    SG
    SG, May 3, 2013
    #3
  4. On 5/3/2013 11:18 AM, SG wrote:
    > Am 03.05.2013 17:12, schrieb Leigh Johnston:
    >> On 03/05/2013 14:33, Victor Bazarov wrote:
    >>> On 5/3/2013 9:16 AM, 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?
    >>>
    >>> I think the type should be the same as of the expression. If the
    >>> expression gives you the const ref, then 'x' will be a const ref and
    >>> will bind to the value returned by the function. Say, 'thatFunction' is
    >>> declared

    >>
    >> If you only "think" you know the answer to the question rather than
    >> actually *knowing* the answer to the question you probably shouldn't
    >> answer the question (on Usenet); it just wastes time.

    >
    > Well, he had the courtosy of admitting that he's not sure about his
    > answer. That's better than pretending to know the answer. :)
    >
    > And Victor, I'm sorry if my wording came across too harsh.
    > All is good.
    >
    > Cheers!


    No worries, mate. It just reminded me why I had LJ in my killfile, and
    to do it again (since this is a relatively new setup)... <sigh>

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 3, 2013
    #4
  5. Victor Bazarov

    Guest

    On Friday, May 3, 2013 10:13:55 AM UTC-5, SG wrote:
    > Am 03.05.2013 15:33, schrieb Victor Bazarov:
    >
    > > On 5/3/2013 9:16 AM, 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?

    >
    > >

    >
    > > I think the type should be the same as of the expression. If the

    >
    > > expression gives you the const ref, then 'x' will be a const ref and

    >
    > > will bind to the value returned by the function.

    >
    >
    >
    > The _type_ of an _expression_ is _never_ a reference type. The declared
    >
    > return type of a function is different from the type of the invocation
    >
    > expression. The return type does however affects the type as well as the
    >
    > value category of the expression. Examples:
    >
    >
    >
    > declared | invocation expression's
    >
    > return type | type | value category
    >
    > -------------+-----------+------------------
    >
    > int | int | prvalue
    >
    > int& | int | lvalue
    >
    > int&& | int | xvalue
    >
    > const int& | const int | lvalue
    >
    >
    >
    > etc.
    >
    >
    >
    > And no, x won't be a reference unless you write auto& x, auto&& x, const
    >
    > auto& x, etc...
    >
    >
    >
    > > declared

    >
    > >

    >
    > > const double& thatFunction();

    >
    > >

    >
    > > then the type of 'x' is going to be const double&.

    >
    >
    >
    > No, it would be just double.
    >
    >


    Looking at your chart above I thought the const
    would carry through.

    Brian
    Ebenezer Enterprises - Children deserve to have a
    father and a mother.
    http://webEbenezer.net
    , May 4, 2013
    #5
  6. Victor Bazarov

    SG Guest

    Am 04.05.2013 06:00, schrieb :
    > On Friday, May 3, 2013 10:13:55 AM UTC-5, SG wrote:
    >
    >> declared | invocation expression's
    >> return type | type | value category
    >> -------------+-----------+------------------
    >> int | int | prvalue
    >> int& | int | lvalue
    >> int&& | int | xvalue
    >> const int& | const int | lvalue
    >>
    >> etc.

    >
    > Looking at your chart above I thought the const
    > would carry through.


    This chart is only about expressions, not template argument deduction.
    Template argument deduction will strip top-level qualifiers in
    "pass-by-value situations", and so will "auto x = ...".

    Cheers!
    SG
    SG, May 4, 2013
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  2. linkswanted
    Replies:
    1
    Views:
    901
  3. SG
    Replies:
    0
    Views:
    179
  4. Balog Pal

    Re: 'auto' and const references

    Balog Pal, May 4, 2013, in forum: C++
    Replies:
    2
    Views:
    252
    Balog Pal
    May 5, 2013
  5. K. Frank
    Replies:
    4
    Views:
    179
Loading...

Share This Page