select_on_container_copy_construction
in my allocator is not called for std:::string
. It works fine when used with a vector tough. Why is the behavior different? Is this a bug in GCC?
Code example with a minimal allocator:
#include <iostream>
#include <vector>
template<class T>
class allocator {
public:
typedef T value_type;
using propagate_on_container_copy_assignment = std::true_type; // for consistency
using propagate_on_container_move_assignment = std::true_type; // to avoid the pessimization
using propagate_on_container_swap = std::true_type; // to avoid the undefined behavior
allocator<T> select_on_container_copy_construction() const {
std::cout << "COPY THROW!!!" << std::endl;
throw std::bad_alloc();
}
T *allocate(const std::size_t n) {
return static_cast<T *>(::operator new(n * sizeof(T)));
}
void deallocate(T *, const std::size_t) {
/*no op*/
}
};
int main()
{
std::basic_string<char, std::char_traits<char>, allocator<char>> s;
auto ss = s; // Does not output anything, select_on_container_copy_construction NOT called.
std::vector<int, allocator<int>> v;
auto vv = v; // Correctly calls select_on_container_copy_construction and exits.
return 0;
}
Aucun commentaire:
Enregistrer un commentaire