Home > database >  What is the purpose of "int[]" here: "std::void_t<int[static_cast<Int>(-1) &
What is the purpose of "int[]" here: "std::void_t<int[static_cast<Int>(-1) &

Time:11-30

This is from an example from a tutorial on std::enable_if.

Here is more context:

// handle signed types
template<typename Int>
auto incr1(Int& target, Int amount)
-> std::void_t<int[static_cast<Int>(-1) < static_cast<Int>(0)]>;

From

  1. Shouldn't std::void_t be accepting a type as template argument?
  2. What is the purpose of int[] in this context?

CodePudding user response:

If static_cast<Int>(-1) < static_cast<Int>(0) yields true, int[static_cast<Int>(-1) < static_cast<Int>(0)] leads to int[1] (true could be converted to int (and then std::size_t) implicitly with value 1), which is an array type.

If static_cast<Int>(-1) < static_cast<Int>(0) yields false, int[static_cast<Int>(-1) < static_cast<Int>(0)] leads to int[0] (false could be converted to int (and then std::size_t) implicitly with value 0), which is an invalid array type and SFINAE would discard the specialization from the overload set. (The size of array must be greater than zero (unless using in new[]-expression)).

CodePudding user response:

int[0] (int[false]) is ill-formed and rejected by SFINAE.

Probably more clear would be:

std::enable_if_t<static_cast<Int>(-1) < static_cast<Int>(0)>
  • Related