detail 命名空间中的 using-directive 是否有问题?
Is a using-directive in a detail namespace problematic?
考虑这个图书馆 header:
#include<vector>
#include<algorithm>
#include<iostream>
namespace Lib {
namespace detail {
using namespace std;
template<class T>
void sort_impl(istream &in,ostream &out) {
vector<T> v;
{
int n;
in >> n;
v.resize(n);
}
for(auto &i : v) cin >> i;
sort(v.begin(),v.end());
for(auto i : v) out << i << endl;
}
}
inline void sort_std() {
detail::sort_impl<int>(std::cin,std::cout);
}
}
在此示例中,detail
命名空间是否成功地将库的客户端(以及库的其余实现)与 using-directive 隔离开来?我对 Why is "using namespace std" considered bad practice? 上的讨论不感兴趣,尽管有些论点甚至适用于 "well contained" using-directives.
请注意,有两个关于相同情况的现有问题,但 using-declarations:
- Using declarations in private namespaces in header files
- Elegant way to prevent namespace poisoning in C++(其中一个答案实际上是对上述 "bad practice" 问题的回答)
这个可以和其中任何一个结合,但是编辑会很严重。
否,detail
命名空间不会将客户端与嵌套的using
指令隔离。 [namespace.udir] 对此非常明确
A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. During unqualified name lookup, the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace. [ Note: In this context, “contains” means “contains directly or indirectly”. — end note ]
一个小例子
#include <iostream>
namespace foo {
namespace detail {
using namespace std;
}
}
int main()
{
foo::detail::cout << "Hello World!\n";
// nothing is stopping me from doing that
using namespace foo::detail;
cout << "Hello World!\n";
}
STL 在他的视频中很好地解释了名称查找的工作原理 Core C++, 1 of n。
您污染了自己的 detail
命名空间,但未污染 Lib
或全局命名空间。因此,假设负责任的成年人正在使用您的图书馆,他们不会有无意的名称冲突:
#include <vector>
namespace Lib {
namespace detail {
using namespace std;
}
}
using namespace Lib;
int main() {
vector<int> v; // This is an error, vector not declared in this scope
}
考虑这个图书馆 header:
#include<vector>
#include<algorithm>
#include<iostream>
namespace Lib {
namespace detail {
using namespace std;
template<class T>
void sort_impl(istream &in,ostream &out) {
vector<T> v;
{
int n;
in >> n;
v.resize(n);
}
for(auto &i : v) cin >> i;
sort(v.begin(),v.end());
for(auto i : v) out << i << endl;
}
}
inline void sort_std() {
detail::sort_impl<int>(std::cin,std::cout);
}
}
在此示例中,detail
命名空间是否成功地将库的客户端(以及库的其余实现)与 using-directive 隔离开来?我对 Why is "using namespace std" considered bad practice? 上的讨论不感兴趣,尽管有些论点甚至适用于 "well contained" using-directives.
请注意,有两个关于相同情况的现有问题,但 using-declarations:
- Using declarations in private namespaces in header files
- Elegant way to prevent namespace poisoning in C++(其中一个答案实际上是对上述 "bad practice" 问题的回答)
这个可以和其中任何一个结合,但是编辑会很严重。
否,detail
命名空间不会将客户端与嵌套的using
指令隔离。 [namespace.udir] 对此非常明确
A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. During unqualified name lookup, the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace. [ Note: In this context, “contains” means “contains directly or indirectly”. — end note ]
一个小例子
#include <iostream>
namespace foo {
namespace detail {
using namespace std;
}
}
int main()
{
foo::detail::cout << "Hello World!\n";
// nothing is stopping me from doing that
using namespace foo::detail;
cout << "Hello World!\n";
}
STL 在他的视频中很好地解释了名称查找的工作原理 Core C++, 1 of n。
您污染了自己的 detail
命名空间,但未污染 Lib
或全局命名空间。因此,假设负责任的成年人正在使用您的图书馆,他们不会有无意的名称冲突:
#include <vector>
namespace Lib {
namespace detail {
using namespace std;
}
}
using namespace Lib;
int main() {
vector<int> v; // This is an error, vector not declared in this scope
}