Home > Mobile >  reference type as type traits / concept argument
reference type as type traits / concept argument

Time:08-13

In the specification-mandated implementation of the concept std::uniform_random_bit_generator, it is required that invoking operator() on an instance of type G satisfying this concept should return the same type as G::min() and G::max().

Why is std::same_as<std::invoke_result_t<G&>> used instead of std::same_as<std::invoke_result_t<G>>? What the difference?

CodePudding user response:

std::invocable<G&> checks whether an lvalue of type G can be invoked (without arguments). std::invocable<G> checks whether a rvalue of type G can be invoked (without arguments).

std::invoke_result_t is equivalently the corresponding return type.

In other words this guarantees that the generator can be declared as a variable and then invoked, e.g.

G g{/*args*/};
auto res = g();

But it does not guarantee that a temporary of type G can be invoked directly, e.g.

auto res = G{/*args*/}();

This is not how a random number generator is commonly used.

  • Related