2774. std::function construction vs assignment

Section: 23.14.13.2.1 [func.wrap.func.con] Status: New Submitter: Barry Revzin Opened: 2016-09-14 Last modified: 2016-10-12

Priority: 3

View other active issues in [func.wrap.func.con].

View all other issues in [func.wrap.func.con].

View all issues with New status.

Discussion:

I think there's a minor defect in the std::function interface. The constructor template is:

template <class F> function(F f);

while the assignment operator template is

template <class F> function& operator=(F&& f);

The latter came about as a result of LWG 1288, but that one was dealing with a specific issue that wouldn't have affected the constructor. I think the constructor should also take f by forwarding reference, this saves a move in the lvalue/xvalue cases and is also just generally more consistent. Should just make sure that it's stored as std::decay_t<F> instead of F.

Is there any reason to favor a by-value constructor over a forwarding-reference constructor?

Proposed resolution: