与32位和64位平台的INT64_T匹配的整数字面形式?
考虑下面的一件代码。是否有一个整数字面形式可以在32位和64位平台上编译?
#include <iostream>
#include <cstdint>
void f(double)
{
std::cout << "double\n";
}
void f(int64_t)
{
std::cout << "int64_t\n";
}
int main()
{
f(0L); // works on 64-bit fails on 32-bit system
f(0LL); // fails on 64-bit but works on 32-bit system
f(int64_t(0)); // works on both, but is ugly...
f(static_cast<int64_t>(0)); // ... even uglier
return 0;
}
在平台上int64_t
是long
,0ll
是另一种类型,而Orderload分辨率不希望它与double
。
当int64_t
是长长
(包括在Windows X64上)时,我们与0L
有同样的问题。
(0ll
是int64_t
在32位和X86-64 Windows ABIS(LLP64)中均使用X86-64 System V Windows ABI(LLP64),这是LP64 ABI。当然,非X86系统可移植的东西会很好。)
Consider a piece of code below. Is there an integer literal that would compile on both 32-bit and 64-bit platforms?
#include <iostream>
#include <cstdint>
void f(double)
{
std::cout << "double\n";
}
void f(int64_t)
{
std::cout << "int64_t\n";
}
int main()
{
f(0L); // works on 64-bit fails on 32-bit system
f(0LL); // fails on 64-bit but works on 32-bit system
f(int64_t(0)); // works on both, but is ugly...
f(static_cast<int64_t>(0)); // ... even uglier
return 0;
}
On platforms where int64_t
is long
, 0LL
is a different type and overload resolution doesn't prefer it vs. double
.
When int64_t
is long long
(including on Windows x64), we have the same problem with 0L
.
(0LL
is int64_t
in both the 32-bit and x86-64 Windows ABIs (LLP64), but other OSes use x86-64 System V which is an LP64 ABI. And of course something portable to non-x86 systems would be nice.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以进行自定义用户定义的文字 for
int64_t
喜欢然后您的函数调用将变成
这种情况,如果您尝试使用文字
-92233720368547775808
,则会为您带来错误的价值。 /45469214/Why-do-the-the-the-the-the-the-int-in-in-rorr-abour-about-abour-amboud-ambiul-function-ove/45469321#45469321“>在这里You can make a custom user defined literal for
int64_t
likeand then your function call would become
This will give you an incorrect value if you try to use the literal
-9223372036854775808
for the reason stated here