C ++ Headaches in the namespace

Well, this question has changed a bit, and I want to try to start (with) the main goals that I am shooting for:

  • Create library code that wraps legacy C-language entities when collecting C ++ resources, initializes, and also provides a basic or better exception guarantee.
  • Enable clients of this code to use it in a very natural C ++ mode without creating a lot of overhead for existing code in order to convert it to using C ++ shell objects (i.e. automatically convert to the corresponding types of obsolete objects, constructors, which use legacy types, etc.)
  • Limit the influence of namespace on library code. Ideally, a library would have several namespaces that provide related functions that limit the scope and impact of using type X declarations of the namespace — much like acceleration libraries do (i.e. use part namespaces to enter only those characters that they wisely want to use and hide those that are implementation details, as well as limit the scope of possible new values ​​to existing characters in order to avoid unexpected implicit conversions to the inside When the user code).
  • Require customers to explicitly request those parts of the library that they actually want to enter into their code base. This goes hand in hand with limiting the impact of including library headers. Client code should have a reasonable level of control over which parts of the library will be automatically used to resolve names when compiling their code.
  • - . , typedefs, . : , , , . , , , "".

...

, C N. C Free. , C , C .

. N, C, , , C, .

, - :

namespace N {

void free(THING * thing);

class C
{
public:
  ... details omitted...
  free()
  { 
    free(m_thing); // <- how best to refer to N::free(THING&)
  }
}

} // namespace N

N:: free (m_thing). . , , ( )?

, N:: free , , . , (, dispose). , , , , , .

. , . , , , ( ) - - , , , .

, , ?

EDIT: , :

namespace Toolbox {
  namespace Windows {

// deallocates the given PIDL
void Free(ITEMIDLIST ** ppidl);

class Pidl
{
public:
    // create empty
    Pidl() : m_pidl(NULL) { }

    // create a copy of a given PIDL
    explicit Pidl(const ITEMIDLIST * pidl);

    // create a PIDL from an IShellFolder
    explicit Pidl(IShellFolder * folder);

    ...

    // dispose of the underlying ITEMIDLIST* so we can be free to manage another...
    void Free();
};

, ITEMIDLIST * CoTaskMemFree(). Pidl , Windows Shell.h, .

, , - COM Windows. Toolbox , , Toolbox:: Windows windows-y, ..

++, , ( ). - koenig ( ITEMIDLIST Toolbox:: Windows), ! . , , , Toolbox, , ( Win32 , , - GLOBAL NS - : / //).

, . : , NS , , , NS, (.. -, )?

: , , , , :

using namespace X {
  code here...
}

​​ , , X , ​​ , .

+5
5

, , , , " ". -, , :

namespace N1 {
  namespace N2 {
    namespace N3 {
      void free(THING * thing);

      class C {
      public:
        free() {
          N3::free(m_Thing); // no need to do N1::N2::
        }
      };
    }
  }
}

[EDIT] . , , . , ++ - - - ITEMIDLIST, , RAII- (, a la ATL -, c_str(), ). free , , , ADL, .

[ # 2]. :

, , NS, (.. -, )?

, , , using namespace ..., ?

+6

:

, free.

- m_thing->Release() - .

+5

::free(m_thing);. free(). , free() N, . , .

0

, . , . factory/abstract factory, ( ) . , RAII.

0

, .

, free-function, , . , , swap...

, , , hiding, MyClass::free N::free, ... , .

:

class MyClass
{
  void swap(MyClass& rhs)
  {
    using std::swap;         // Because int, etc... are not in std
    swap(m_foo, rhs.m_foo);
    swap(m_bar, rhs.m_bar);
  }
};

inline void swap(MyClass& lhs, MyClass& rhs) { lhs.swap(rhs); }

, , "" , .

I prefer this approach to explicitly naming and usually boltovki ad usingand typedefin the top part of the method, so that the actual code is not inflated.

0
source

All Articles