How do we test if an expression of a certain type can be invoked with a prvalue?

后端 未结 2 1367
日久生厌
日久生厌 2021-02-05 13:05

With c++17 we have fancy new is_invocable and fancy new prvalues that aren\'t really values.

This permits you to create an object without having to first logically const

2条回答
  •  轮回少年
    2021-02-05 13:47

    You are misusing the Invocable concept. This concept means nothing more than the ability to use std::invoke on the given function and the provided arguments.

    You can't do std::invoke(f, no_move(1)), as this would provoke a copy/move of the forwarded argument. It is impossible for a prvalue to be used as a parameter through a forwarded call like invoke. You can pass a prvalue to the forwarding call, but the eventual call to the given function will get an xvalue.

    This is a good reason to avoid using immobile types as value parameters in functions. Take them by const& instead.

    C++ does not have a type trait to see if a function can be called with specific parameters in the way that you want.

提交回复
热议问题