英文:
Why there is error message about "__int64" when it's not even used in my code?
问题
I wrote the code below:
#define int64_t long long int
typedef long long int64_t; //此行是第2行
void main()
{
int64_t a;
}
当我编译这个程序时,编译器显示了一个错误消息:在第2行,'__int64' 后面跟着 'long' 是非法的。
但奇怪的是,我的代码中没有 '__int64',只有 'int64_t',但 'int64_t' 和 '__int64' 拼写不同,因此它们是完全不同的变量。是的,关于一个从未出现在我的代码中的变量的错误发生了,这很奇怪。第2行有什么问题,这个行是如何引发这个错误的?
谢谢阅读,希望得到您宝贵的答案。
<details>
<summary>英文:</summary>
I wrote the code below:
#define int64_t long long int
typedef long long int64_t; //this line is line 2
void main()
{
int64_t a;
}
When I complied this program, the compiler displayed an error message: in line 2, '__int64' followed by 'long' is illegal.
But the strange thing is that there is no "__int64" in my code, only "int64_t", but "int64_t" and "__int64" are spelled differently, so they are totally different variables. Yes, it is wired that an error about a variable which never appears in my code occurred. What is wrong about line 2, and how does this line cause this error?
Thank you for reading and hope to receive your valuable answers.
</details>
# 答案1
**得分**: 8
在C++标准添加`long long`作为具有至少64位的标准类型之前,Microsoft在其编译器中添加了一个名为`__int64`的类型,以提供相同的基本功能,但名称是符合C的扩展(即以下划线后跟另一个下划线或大写字母开头的名称保留给实现)。
当C++委员会开始将`long long`添加到C++中时,Microsoft似乎已经将其实现为其现有的`__int64`类型的别名。因此,当您使用`long long`时,编译器在内部将其视为`__int64`。
在您的代码中,在宏展开后,您有:
```c++
typedef long long long long int;
在解析此代码时,编译器将第一个long long
视为__int64
。然后跟着另一个long
,解析失败。当它打印出错误消息时,它使用了__int64
作为64位类型的内部名称来打印出long long
。
英文:
Back before the C++ standard added long long
as a standard type with a minimum of 64 bits, Microsoft added a type named __int64
to their compiler to provide the same basic capability, but with a name that was a conforming extension to C (i.e., names starting with an underscore followed by another underscore or an upper-case letter are reserved for the implementation).
When the C++ committee got around to adding long long
to C++, Microsoft seems to have implemented it as an alias for their existing __int64
type. So, when you use long long
, the compiler internally "thinks" of that as meaning __int64
.
In your code, after the macro is expanded, you have:
typedef long long long long int;
In parsing this the compiler treats the first long long
as meaning __int64
. That's then followed by another long
, and parsing fails. When it prints out the error message, it prints out long long
using its internal name for that 64-bit type (__int64
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论