static_assert'ion long 和 int 是同一类型

static_assert'ion that a long and int are the Same Type

所以我从一个 API 中获取一个变量,我们将其称为 long foo 并将其传递给另一个 API,后者将其作为值:int bar.

我在 in which these are effectively the same thing: https://docs.microsoft.com/en-us/cpp/cpp/data-type-ranges?view=vs-2017

但这会触发:

static_assert(is_same_v<decltype(foo), decltype(bar)>);

因为尽管它们实际上相同,但它们不是同一类型。除了使用数字限制库将 longint 匹配外,是否有解决方法?

longint 是不同的基本类型。即使它们的大小相同,它们也不是同一类型,因此 is_same_v 永远不会是 true。如果您愿意,可以检查它们的尺寸是否相同,然后继续

static_assert(sizeof(foo) == sizeof(bar));

你甚至可以确保 foobar 是像

这样的整数类型
static_assert(sizeof(foo) == sizeof(bar) && 
              std::is_integral_v<decltype(foo)> && 
              std::is_integral_v<decltype(bar)>);

您还可以确保它们具有与

相同的签名
static_assert(sizeof(foo) == sizeof(bar) && 
             std::is_integral_v<decltype(foo)> && 
             std::is_integral_v<decltype(bar)> &&
             std::is_signed_v<decltype(foo)> == std::is_signed_v<decltype(bar)>);