这个伪代码是从GotW #53获得的,标题是“A Not-So-Good Long-Term Solution”。几个小时以来,我一直在试图理解作者在说什么,特别是与下面以“//错误:潜在......”开头的评论有关,但无济于事。我真的很感激这方面的一些帮助。
// 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 )
{
// ...
}
}