简体   繁体   English

unsigned long long(10) 无法在 clang 和 gcc 上编译

[英]unsigned long long(10) fails to compile on clang and gcc

The following code is rejected by both clang and gcc but accepted by msvc:以下代码被 clang 和 gcc 拒绝,但被 msvc 接受:

#include <iostream>

int main() 
{
    std::cout << unsigned long long(10);
}

The error is错误是

error: expected primary-expression before 'unsigned'错误:“无符号”之前的预期主表达式

godbolt神箭

This should compile, right?这应该编译,对吧?

No, what you have shown should NOT compile.不,您所展示的内容不应编译。 See Explicit type conversions on cppreference.com for details.有关详细信息,请参阅 cppreference.com 上的显式类型转换

In a function-style cast, spaces are not allowed in the type name.在函数风格的强制转换中,类型名称中不允许有空格。 For such types, you would need to use a C-style or C++-style cast instead, eg:对于此类类型,您需要使用 C 样式或 C++ 样式的强制转换,例如:

std::cout << ((unsigned long long)10);
or
std::cout << static_cast<unsigned long long>(10);

Otherwise, use a type alias instead, eg:否则,请改用类型别名,例如:

using ull = unsigned long long; // C++11 and later
or
typedef unsigned long long ull; // pre-C++11

std::cout << ull(10);

Note, the <cstdint> header may have a uint64_t type you can use, eg:请注意, <cstdint>标头可能具有您可以使用的uint64_t类型,例如:

#include <cstdint>

std::cout << uint64_t(10);
or
std::cout << ((uint64_t)10);
or
std::cout << static_cast<uint64_t>(10);

That being said, for integer literals, you can alternatively use the ULL suffix (C++11 and later), eg:话虽如此,对于整数文字,您也可以使用ULL后缀(C++11 及更高版本),例如:

std::cout << 10ULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM