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

92 Views Asked by At

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?
2

There are 2 best solutions below

3
leslie.yao On BEST ANSWER

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)).

0
Jarod42 On

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)>