为什么我的代码中根本没有使用”__int64″,却出现了关于”__int64″的错误消息?

huangapple go评论78阅读模式
英文:

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, &#39;__int64&#39; followed by &#39;long&#39; is illegal. 

But the strange thing is that there is no &quot;__int64&quot; in my code, only &quot;int64_t&quot;, but &quot;int64_t&quot; and &quot;__int64&quot; 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).

huangapple
  • 本文由 发表于 2023年6月15日 13:47:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定