"using namespace" 子句在什么范围内有效?
In what scopes is the "using namespace" clause valid?
我曾经 heard/read(我忘记了引用;无法引用)“using namespace
”子句在任何范围内都有效,但它似乎在 class范围:
// main.cpp
#include <iostream>
namespace foo
{
void func() { std::cout << __FUNCTION__ << std::endl; }
};
class Foo
{
using namespace foo;
public:
Foo() { func(); }
};
int main( int argc, char* argv[] )
{
Foo f;
}
.
$ g++ --version
g++ (GCC) 8.3.1 20190223 (Red Hat 8.3.1-2)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
.
$ g++ -g ./main.cpp
./main.cpp:12:7: error: expected nested-name-specifier before 'namespace'
using namespace foo;
^~~~~~~~~
./main.cpp: In constructor 'Foo::Foo()':
./main.cpp:15:13: error: 'func' was not declared in this scope
Foo() { func(); }
^~~~
./main.cpp:15:13: note: suggested alternative:
./main.cpp:7:8: note: 'foo::func'
void func() { std::cout << __FUNCTION__ << std::endl; }
^~~~
$
$ g++ --std=c++11 -g ./main.cpp
./main.cpp:12:7: error: expected nested-name-specifier before 'namespace'
using namespace foo;
^~~~~~~~~
./main.cpp: In constructor 'Foo::Foo()':
./main.cpp:15:13: error: 'func' was not declared in this scope
Foo() { func(); }
^~~~
./main.cpp:15:13: note: suggested alternative:
./main.cpp:7:8: note: 'foo::func'
void func() { std::cout << __FUNCTION__ << std::endl; }
^~~~
class Foo
的以下变体会导致相同的编译器错误:
class Foo
{
using namespace ::foo;
public:
Foo()
{
func();
}
};
class Foo
的以下变体不会导致编译器错误或警告:
class Foo
{
public:
Foo()
{
using namespace foo;
func();
}
};
.
class Foo
{
public:
Foo()
{
foo::func();
}
};
我(不正确?)根据阅读 this and this 等帖子对编译器错误的理解是,该错误本质上需要使用的命名空间的完整范围,即我在第一个变体中尝试 class Foo
, 以上.
请注意:除了显式使用 --std=c++11
编译器标志外,所使用的编译器版本远高于不会遇到此错误所需的最低版本(不显式使用 --std=c++11
编译器标志),根据著名的 Stack Overflow Q&A。
这个编译器错误是什么意思(如果与我的上述理解不同)在这种情况下?(我的使用似乎不同比两个著名的 Stack Overflow 问答)。
一般来说:"using namespace" 指令在哪些范围内有效?
Using-directives are allowed only in namespace scope and in block scope.
因此您可以在命名空间(包括全局命名空间)或代码块中使用它们。 class 声明两者都不是。
我曾经 heard/read(我忘记了引用;无法引用)“using namespace
”子句在任何范围内都有效,但它似乎在 class范围:
// main.cpp
#include <iostream>
namespace foo
{
void func() { std::cout << __FUNCTION__ << std::endl; }
};
class Foo
{
using namespace foo;
public:
Foo() { func(); }
};
int main( int argc, char* argv[] )
{
Foo f;
}
.
$ g++ --version
g++ (GCC) 8.3.1 20190223 (Red Hat 8.3.1-2)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
.
$ g++ -g ./main.cpp
./main.cpp:12:7: error: expected nested-name-specifier before 'namespace'
using namespace foo;
^~~~~~~~~
./main.cpp: In constructor 'Foo::Foo()':
./main.cpp:15:13: error: 'func' was not declared in this scope
Foo() { func(); }
^~~~
./main.cpp:15:13: note: suggested alternative:
./main.cpp:7:8: note: 'foo::func'
void func() { std::cout << __FUNCTION__ << std::endl; }
^~~~
$
$ g++ --std=c++11 -g ./main.cpp
./main.cpp:12:7: error: expected nested-name-specifier before 'namespace'
using namespace foo;
^~~~~~~~~
./main.cpp: In constructor 'Foo::Foo()':
./main.cpp:15:13: error: 'func' was not declared in this scope
Foo() { func(); }
^~~~
./main.cpp:15:13: note: suggested alternative:
./main.cpp:7:8: note: 'foo::func'
void func() { std::cout << __FUNCTION__ << std::endl; }
^~~~
class Foo
的以下变体会导致相同的编译器错误:
class Foo
{
using namespace ::foo;
public:
Foo()
{
func();
}
};
class Foo
的以下变体不会导致编译器错误或警告:
class Foo
{
public:
Foo()
{
using namespace foo;
func();
}
};
.
class Foo
{
public:
Foo()
{
foo::func();
}
};
我(不正确?)根据阅读 this and this 等帖子对编译器错误的理解是,该错误本质上需要使用的命名空间的完整范围,即我在第一个变体中尝试 class Foo
, 以上.
请注意:除了显式使用 --std=c++11
编译器标志外,所使用的编译器版本远高于不会遇到此错误所需的最低版本(不显式使用 --std=c++11
编译器标志),根据著名的 Stack Overflow Q&A。
这个编译器错误是什么意思(如果与我的上述理解不同)在这种情况下?(我的使用似乎不同比两个著名的 Stack Overflow 问答)。
一般来说:"using namespace" 指令在哪些范围内有效?
Using-directives are allowed only in namespace scope and in block scope.
因此您可以在命名空间(包括全局命名空间)或代码块中使用它们。 class 声明两者都不是。