将函数声明为静态,然后声明为非静态:这是标准的吗?

发布于 2024-08-15 19:51:35 字数 407 浏览 1 评论 0原文

我注意到一种非常奇怪的行为,如果是标准的,我会很乐意利用它(我想用它做的事情解释起来相当复杂,并且与问题无关)。

行为是:

static void name();
void name() {
    /* This function is now static, even if in the declaration
     * there is no static keyword. Tested on GCC and VS. */
}

奇怪的是,逆运算会产生编译时错误:

void name();
static void name() {
    /* Illegal */
}

那么,这是标准吗?我可以期望其他编译器也有相同的行为吗?谢谢!

I noticed a very curious behavior that, if standard, I would be very happy to exploit (what I'd like to do with it is fairly complex to explain and irrelevant to the question).

The behavior is:

static void name();
void name() {
    /* This function is now static, even if in the declaration
     * there is no static keyword. Tested on GCC and VS. */
}

What's curious is that the inverse produces a compile time error:

void name();
static void name() {
    /* Illegal */
}

So, is this standard and can I expect other compilers to behave the same way? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帝王念 2024-08-22 19:51:35

C++标准:

7.1.1/6:“在名称空间范围中声明的名称,没有
存储类说明符具有外部
除非有内部链接,否则链接
因为之前的声明”[或者除非它是 const]。

在第一种情况下,name 是在命名空间范围(特别是全局命名空间)中声明的。因此,第一个声明会更改第二个声明的链接 逆向声明

被禁止,因为:

7.1.1/7:“给定的连续声明所暗示的链接
实体应同意”。

因此,在您的第二个示例中,第一个声明具有外部链接(通过 7.1.1/6),第二个声明具有内部链接(明确),并且这些声明不同意。

还询问了 C,并且我想这是同样的事情,但我这里有一本 C++ 书,而你可以像我一样在线查找 C 标准草案;-)

C++ standard:

7.1.1/6: "A name declared in a namespace scope without a
storage-class-specifier has external
linkage unless it has internal linkage
because of a previous declaration" [or unless it's const].

In your first case, name is declared in a namespace scope (specifically, the global namespace). The first declaration therefore alters the linkage of the second declaration.

The inverse is banned because:

7.1.1/7: "The linkages implied by successive declarations for a given
entity shall agree".

So, in your second example, the first declaration has external linkage (by 7.1.1/6), and the second has internal linkage (explicitly), and these do not agree.

You also ask about C, and I imagine it's the same sort of thing. But I have the C++ book right here, whereas you're as capable of looking in a draft C standard online as I am ;-)

鸠书 2024-08-22 19:51:35

声明函数时,将自动使用您在函数原型上放置的(或隐含的)限定符。

因此,在第二种情况下,原型上缺少 static 意味着该函数被定义为非静态,然后当它后来被声明为静态时,这是一个错误。

如果您在原型中省略返回类型,则默认值为 int,然后您将再次收到 void 返回类型的错误。同样的情况也发生在 __crtapi__stdcall 以及 __declspec() (在 Microsoft C 编译器中)。

Qualifiers that you put on the function prototype (or that are implied) are automatically used when the function is declared.

So in your second case the lack of static on the prototype meant that the function was defined as NOT static, and then when it was later declared as static, that was an error.

If you were to leave off the return type in the prototype, then the default would be int and then you would get an error again with the void return type. The same thing happens with __crtapi and __stdcall and __declspec() (in the Microsoft C compiler).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文