C ++ Perfect Function Forwarding

This is a question for this question.

A 2002 paper in function transfer function in C ++ makes the following observation:

This is the currently used method by Boost.Bind and Boost.Lambda:

template<class A1, class A2, class A3> void f(A1 & a1, A2 & a2, A3 & a3) { return g(a1, a2, a3); } 

Its main drawback is that it cannot forward the non-constant value of r. the output of the argument creates a non-constant link, and the link cannot link to the argument. It makes innocent examples like

 int main() { f(1, 2, 3); } 

fail (violates C1).

I see the challenge is failing, but is this explanation true? Are literals 1, 2, 3 constants?

+4
source share
1 answer

Are not the literals 1, 2, 3 const rvalues?

No, they are just rvalues ​​of type int. According to the C ++ standard, the values ​​of primitive types cannot be const-qual.

The call fails because they are rvalues ​​- non-const links cannot be associated with rvalues.

The call will be OK if the functions took const A1 &, const A2&, const A3& , but in this case the function will not be able to change the arguments.

Edit: Link to my first statement from the C ++ 2003 standard: (3.10.9)

Class values ​​can have cv-qualified types; nonclass values ​​always have cv-unqualified types. Values ​​should always have full types or void type; in addition to these types, lvalues ​​can also be incomplete types.

+6
source

Source: https://habr.com/ru/post/1342852/


All Articles