我自己想出了这个解决方案。我目前只关心整数,但更通用的解决方案将不胜感激。任何反馈?谢谢。
#include <limits>
using namespace std;
typedef unsigned long long UINT64;
typedef unsigned long long UINT_64;
typedef unsigned int UINT_32;
typedef unsigned long int UINT32;
template<bool> struct static_assert;
template<> struct static_assert<true> {};
#define CHECK_INTEGER_TYPES(T, U) (numeric_limits<T>::is_specialized && numeric_limits<U>::is_specialized && numeric_limits<T>::is_integer && numeric_limits<U>::is_integer && (numeric_limits<T>::is_signed == numeric_limits<U>::is_signed) && (numeric_limits<T>::digits == numeric_limits<U>::digits))
int main()
{
static_assert<(CHECK_INTEGER_TYPES(UINT64, UINT_64))>(); // pass
static_assert<CHECK_INTEGER_TYPES(UINT64, UINT64)>(); // pass
static_assert<CHECK_INTEGER_TYPES(UINT64, UINT_32)>(); // fail
static_assert<CHECK_INTEGER_TYPES(UINT32, UINT32)>(); // pass
static_assert<CHECK_INTEGER_TYPES(UINT32, UINT_32)>(); // pass
}