以下代码被clang和gcc拒绝,但被msvc接受:
#include <iostream>
int main()
{
std::cout << unsigned long long(10);
}
错误是
错误:“未签名”之前的预期主表达式
这个应该编译吧?
不,您所显示的内容不应编译。有关详细信息,请参见cppreference.com上的显式类型转换。
在函数样式转换中,类型名称中不允许使用空格。对于此类类型,您将需要使用C样式或C ++样式强制转换,例如:
std::cout << ((unsigned long long)10);
or
std::cout << static_cast<unsigned long long>(10);
否则,请改用类型别名,例如:
using ull = unsigned long long; // C++11 and later
or
typedef unsigned long long ull; // pre-C++11
std::cout << ull(10);
注意,<cstdint>
标题可能具有uint64_t
您可以使用的类型,例如:
#include <cstdint>
std::cout << uint64_t(10);
or
std::cout << ((uint64_t)10);
or
std::cout << static_cast<uint64_t>(10);
话虽如此,对于整数文字,您也可以使用ULL
后缀(C ++ 11和更高版本),例如:
std::cout << 10ULL;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句