作者想在GotW#53中说些什么

What the author is trying to say in GotW #53?

本文关键字:什么 GotW#53      更新时间:2023-10-16

此伪代码是从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定义

作者说,这段代码的问题在于,由于这段代码在其他应用程序中使用,根据其他头文件中使用的名称空间,可能会有多个名称相同的东西("潜在的未来名称空间歧义")。如果他们说应该使用不同的名称空间,那么相同的名称可能不会指向作者最初的意图。