Код: Выделить всё
template
is_integral
{
static constexpr bool value = false;
}
template
is_integral
{
static constexpr bool value = true;
}
// do this for like 10 other types, at least
< /code>
Я пытался обнаружить целые числа с разумной надежностью, используя две вещи: < /p>
[list]
[*](T(3)/T(2)) == T(1)
Код: Выделить всё
(T(0) & T(1)) == T(0)
можно добавить. Но у меня возникают проблемы с этим. Для этого для компиляции мне нужно избегать ошибок, когда/ или и не определены для t .
Я попробовал это:
Код: Выделить всё
#if __cplusplus > 199711L
namespace impl
{
// type that is returned for X op Y when operator op is missing
struct NoOperator {};
// Out of line operator for any X & Y
template NoOperator operator& (const T&, const Arg&);
template
struct BitAndExists
{
// check if the result of bit and is our NoOperator type
enum { value = !is_same::value };
};
}
#endif
namespace impl
{
template
constexpr bool has_integer_bit_and()
{
if constexpr (impl::BitAndExists::value && !is_floating_point::value)
{
return (T(0) & T(1)) == T(0);
}
else
{
return false;
}
}
template
struct integral_checks {
static constexpr bool bit_and_check = has_integer_bit_and();
static constexpr bool round_check = (T(3)/T(2)) == T(1);
static constexpr bool value = bit_and_check && round_check;
};
template
struct integral_checks {
static constexpr bool bit_and_check = false;
static constexpr bool round_check = false;
static constexpr bool value = false;
};
template
struct integral_checks {
static constexpr bool bit_and_check = false;
static constexpr bool round_check = false;
static constexpr bool value = false;
};
}
template
struct is_integral
{
static constexpr bool value = impl::integral_checks::value;
};
< /code>
Но я получаю несколько ошибок: < /p>
error: invalid operands of types 'float' and 'float' to binary 'operator&'
101 | enum { value = !is_same::value };
< /code>
Так что это означает, что трюк оператора не работает, как задумано. < /p>
Constexpr variable 'bit_and_check' must be initialized by a constant expression
Вот полный код образец: https://godbolt.org/z/ys9bqkq4a флаг /br/> atm. Я понимаю, что утиный тип, как это, может иметь некоторые неприятные непреднамеренные последствия. Но я хочу учиться на ошибках выше.
Подробнее здесь: https://stackoverflow.com/questions/795 ... pecializat