Thread: Why a function with const type reference argument doesn't use it as a rvalue ?

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    Why a function with const type reference argument doesn't use it as a rvalue ?

    Hello,

    Given a class with [] operators for both lvalue and rvalue:
    Code:
    class A
    {
      public:
        double items[10];
        double operator [] (unsigned int i)
        {
            cout << "rvalue operator" << endl;
            return items[i];
        }
        double &operator [] (unsigned int i) const
        {
            cout << "lvalue operator" << endl;
            return items[i];
        }
    };
    and a function with a const A& as argument
    Code:
    void theFunc(const A &a)
    {
    }
    The following lines have the expected behavior:
    Code:
    A a;
    double v = a[0]; // calls the rvalue [] operator
    a[1] = 10; // calls the lvalue [] operator
    But I wouldn't have expected this one:
    Code:
    theFunc(a); // calls the rvalue operator ! Why ?
    Obviously, if I cast the argument with something like this
    Code:
    theFunc(static_cast<const A&>(a));
    , it works as expected, but I'd rather use a cleaner solution.

    Why the rvalue isn't used in that case ?
    Does anyone know a solution to this issue ?

    Thanks.

    Ed

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    With the code snippets you have shown, calling theFunc(a) cannot call either of the [] operators. It simply passes a const reference to a.

    If you're getting some other result, it probably means theFunc() is overloaded (i.e. there is a version which takes some type of argument, other than what you have shown).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    [EDIT]
    You are absolutely right, sorry for that error.
    The idea was obviously the following:
    Code:
    void theFunc(const double &d)
    {
    }
    and the difference between
    Code:
    theFunc(a[0]); // which uses the lvalue
    
    // and
    
    theFunc(static_cast<const double&>(a[0])); // which uses the rvalue
    Last edited by edlep; 10-17-2013 at 06:02 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The compiler avoids creating temporaries if possible, in order to pass an argument by reference. The operator that returns a reference is therefore a better match than one which returns by value.

    The static_cast<> forces the compiler to generate a const reference to the value returned by your "rvalue" form.

    A better solution would be to pass theFunc()'s argument by value.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Grumpy,
    Thanks for your answer.
    What do you mean by
    Quote Originally Posted by grumpy View Post
    pass theFunc()'s argument by value.
    ?


    By the way, I realize that I also made ​​a mistake in the definition of my example class (the const is on the wrong operator) ... I should sleep a bit before posting.
    The right definition is:

    Code:
    class A
    {
      public:
        double items[10];
        double operator [] (unsigned int i) const
        {
            cout << "rvalue operator" << endl;
            return items[i];
        }
        double &operator [] (unsigned int i)
        {
            cout << "lvalue operator" << endl;
            return items[i];
        }
    };

    Sorry for the confusion of my post.
    Last edited by edlep; 10-17-2013 at 07:04 AM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by edlep View Post
    Grumpy,
    Thanks for your answer.
    What do you mean by pass theFunc()'s argument by value ?
    You are currently passing the argument to theFunc by reference:
    Code:
    void theFunc(const double &d)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-23-2011, 11:39 PM
  2. const reference pass as argument?
    By patiobarbecue in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2009, 12:33 AM
  3. const qualifier used on a function argument
    By hzmonte in forum C Programming
    Replies: 27
    Last Post: 04-18-2006, 11:08 PM
  4. Error: initialization of non-const reference type
    By JerryL in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2004, 07:43 PM
  5. type & getReference() const doesn't work.
    By Cristian Negres in forum C++ Programming
    Replies: 8
    Last Post: 11-28-2002, 11:00 AM