faq.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ++++++++++++++++++++++++++++++++++
  2. |Boost| Pointer Container Library
  3. ++++++++++++++++++++++++++++++++++
  4. .. |Boost| image:: boost.png
  5. FAQ
  6. ===
  7. .. contents:: :local:
  8. Calling ``assign()`` is very costly and I do not really need to store cloned objects; I merely need to overwrite the existing ones; what do I do?
  9. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  10. Call ``std::copy( first, last, c.begin() );``.
  11. Which mutating algorithms are safe to use with pointers?
  12. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  13. Any mutating algorithm that moves elements around by swapping them. An
  14. important example is ``std::sort()``; examples of unsafe algorithms are
  15. ``std::unique()`` and ``std::remove()``.
  16. .. That is why these algorithms are
  17. provided as member functions.
  18. Why does ``ptr_map<T>::insert()/replace()`` take two arguments (the key and the pointer) instead of one ``std::pair``? And why is the key passed by non-const reference?
  19. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  20. This is the only way the function can be implemented in an exception-safe
  21. manner; since the copy-constructor of the key might throw, and since
  22. function arguments are not guaranteed to be evaluated from left to right,
  23. we need to ensure that evaluating the first argument does not throw.
  24. Passing the key as a reference achieves just that.
  25. When instantiating a pointer container with a type ``T``, is ``T`` then allowed to be incomplete at that point?
  26. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  27. No. This is a distinct property of ``shared_ptr`` which implies some overhead.
  28. However, one can leave ``T`` incomplete in the header file::
  29. // foo.hpp
  30. class Foo { ... };
  31. new_clone( const Foo& ) { ... }
  32. delete_clone( const Foo* ) { ... }
  33. // x.hpp
  34. class Foo; // Foo is incomplete here
  35. class X { ptr_deque<Foo> container; ... }
  36. // x.cpp
  37. #include <x.hpp>
  38. #include <foo.hpp> // now Foo is not incomplete anymore
  39. ...
  40. Why do iterator-range inserts give the strong exception-safety guarantee?
  41. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  42. Is this not very inefficient? It is because it is actually affordable to
  43. do so; the overhead is one heap-allocation which is relatively small
  44. compared to cloning N objects.
  45. What is the _`polymorphic class problem`?
  46. +++++++++++++++++++++++++++++++++++++++++
  47. The problem refers to the relatively troublesome way C++ supports Object
  48. Oriented programming in connection with containers of pointers to
  49. polymorphic objects. In a language without garbage collection, you end up
  50. using either a container of smart pointers or a container that takes
  51. ownership of the pointers. The hard part is to find a safe, fast and
  52. elegant solution.
  53. Are the pointer containers faster and do they have a better memory footprint than a container of smart pointers?
  54. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  55. The short answer is yes: they are faster and they do use less memory; in
  56. fact, they are the only way to obtain the zero-overhead hallmark of C++.
  57. Smart pointers usually have one word or more of memory overhead per
  58. pointer because a reference count must be maintained. And since the
  59. reference count must be maintained, there is also a runtime-overhead. If
  60. your objects are big, then the memory overhead is often negligible, but if
  61. you have many small objects, it is not. Further reading can be found in
  62. these references: `[11] <ptr_container.html#references>`_ and `[12] <ptr_container.html#references>`_.
  63. When the stored pointers cannot be ``0``, how do I allow this "empty" behavior anyway?
  64. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  65. Storing a null-pointer among a list of pointers does not fit well into the Object Oriented paradigm.
  66. The most elegant design is to use the Null-Object Pattern where one basically makes a concrete
  67. class with dummy implementations of the virtual functions. See `[13] <ptr_container.html#references>`_ for details.
  68. .. raw:: html
  69. <hr>
  70. :Copyright: Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see LICENSE_1_0.txt__).
  71. __ http://www.boost.org/LICENSE_1_0.txt