作者想在GotW#53中说些什么
What the author is trying to say in GotW #53?
此伪代码是从GotW#53获得的,副标题为"不太好的长期解决方案"。几个小时以来,我一直在试图理解作者在说什么,特别是关于下面以"//错误:潜在…"开头的评论,但无济于事。我真的很感谢在这方面的帮助。
// Example 2c: Bad long-term solution (or, Why to
// avoid using declarations in
// headers, even not at file scope)
//
//--- file x.h ---
//
#include "y.h" // declares MyProject::Y and adds
// using declarations/directives
// in namespace MyProject
#include <deque>
#include <iosfwd>
namespace MyProject
{
using std::deque;
using std::ostream;
// or, "using namespace std;"
ostream& operator<<( ostream&, const Y& );
int f( const deque<int>& );
}
//--- file x.cpp ---
//
#include "x.h"
#include "z.h" // declares MyProject::Z and adds
// using declarations/directives
// in namespace MyProject
// error: potential future name ambiguities in
// z.h's declarations, depending on what
// using declarations exist in headers
// that happen to be #included before z.h
// in any given module (in this case,
// x.h or y.h may cause potential changes
// in meaning)
#include <ostream>
namespace MyProject
{
ostream& operator<<( ostream& o, const Y& y )
{
// ... uses Z in the implementation ...
return o;
}
int f( const deque<int>& d )
{
// ...
}
}
His说不要在头文件中使用using
指令。例如:假设我们有两个文件x.h和z.h,其中包含这些声明:
// x.h
namespace MyProject
{
using std::deque;
using std::ostream;
....
};
// z.h
namespace MyProject
{
using mystd::deque;
using mystd::ostream;
....
};
问题是:在您的示例中,将调用哪个ostream对象?
// x.cpp
#include "x.h"
#include "z.h"
#include <ostream>
namespace MyProject
{
ostream& operator<<( ostream& o, const Y& y )
{
// ... uses Z in the implementation ...
return o;
}
int f( const deque<int>& d )
{
// ...
}
}
您想调用x.h
定义,但由于includes的顺序,它将调用z.h
includes定义
作者说,这段代码的问题在于,由于这段代码在其他应用程序中使用,根据其他头文件中使用的名称空间,可能会有多个名称相同的东西("潜在的未来名称空间歧义")。如果他们说应该使用不同的名称空间,那么相同的名称可能不会指向作者最初的意图。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- GotW #88 中的"It doesn’t work for references that are members of objects"是什么意思?
- 作者想在GotW#53中说些什么