以下代码可以使用 MSVC(v19.38) 编译,但使用 GCC(13.2) 和 Clang(17.0.1) 编译失败。但旧版本的 GCC,例如 11.2,可以正常工作。
#include <type_traits>
#ifdef MY_INT
template<typename T, T V> struct my_int
{
static constexpr T value = V;
using value_type = T;
constexpr operator value_type() const noexcept { return value; }
constexpr value_type operator()() const noexcept { return value; }
};
template<auto V> using constant_int_t = my_int<decltype(V), V>;
#else
template<auto V> using constant_int_t = std::integral_constant<decltype(V), V>;
#endif
template<typename T1, typename T2>
using add_t = decltype(T1{} + T2{});
template<typename T1, typename T2>
constexpr …Run Code Online (Sandbox Code Playgroud)