gpt4 book ai didi

c++ - 函数定义为限定 id

转载 作者:行者123 更新时间:2023-11-30 01:52:53 25 4
gpt4 key购买 nike

为什么下面的代码编译成功:

#include <stdio.h>

namespace B
{
void foo();
int i=3;
}

void B::foo()
{
i=65;
}
int main(){ }

以下未编译成功:

#include <stdio.h>

namespace B
{
void foo()
{
i=65;//error: ‘i’ was not declared in this scope
}
int i=3;
}

int main(){ }

我想从目前澄清的标准中找到引用。

最佳答案

因为你想要引用 3.4.1 [basic.unqual.lookup]:

/4 A name used in global scope, outside of any function, class or user-declared namespace, shall be declared before its use in global scope.

/6 A name used in the definition of a function following the function’s declarator-id28 that is a member of namespace N (where, only for the purpose of exposition, N could represent the global scope) shall be declared before its use in the block in which it is used or in one of its enclosing blocks (6.3) or, shall be declared before its use in namespace N or, if N is a nested namespace, shall be declared before its use in one of N’s enclosing namespaces.

关于c++ - 函数定义为限定 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23849100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com