Passing temporaries as non-const references in C++

前端 未结 3 1071
长情又很酷
长情又很酷 2021-02-03 11:41

I have the following piece of code, as an example dec_proxy attempts to reverse the effects of the increment operator upon the type that is executed in a complex function call f

3条回答
  •  忘了有多久
    2021-02-03 12:13

    Taking Stephen's advice, you should look at the answer to How come a non-const reference cannot bind to a temporary object? and simply add a member function that returns a reference dec_proxy, e.g.:

    dec_proxy &ref() { return *this; }

    and call foo:

    foo(
        dec_proxy(i).ref(), 
        dec_proxy(j).ref(), 
        dec_proxy(k).ref());
    

    I'm pretty sure that compiles.

提交回复
热议问题