Perfectly capturing a perfect forwarder (universal reference) in a lambda

前端 未结 1 464
遇见更好的自我
遇见更好的自我 2020-12-17 03:38

So I have a perfect forwarder, and I want to appropriately capture it in a lambda, such that R-values are copied in, and L-values are captured by reference. However simply

1条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 04:14

    You may use the following:

    [test = std::conditional_t<
                 std::is_lvalue_reference::value,
                 std::reference_wrapper>,
                 T>{std::forward(t)}]
    

    Live Demo

    but providing helper function seems more readable

    0 讨论(0)
提交回复
热议问题