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