A long story short, I'm working on a container that can contain either copy constructable objects, or those that don't. Something like:
using AllocTraits = std::allocator_traits<Alloc>;
template <typename T>
void foo(T *to T *from, size_t n, Alloc alloc) {
for (size_t i = 0; i < n; ++i) {
AllocTraits::construct(alloc, to++, *from++);
}
}
The problem I have is that my T
can support either copy constructable classes or moveable classes. So, I moved to a slightly different approach:
using AllocTraits = std::allocator_traits<Alloc>;
template <typename T>
void foo(T *to T *from, size_t n, Alloc alloc) {
auto mov_to = std::make_move_iterator(to);
for (size_t i = 0; i < n; ++i) {
AllocTraits::construct(alloc, mov_to++, *from++);
}
}
But this will call T::(T&&)
, when I'd rather call T::T(const T&)
if the type supports it.
How can I write foo
so that it will call T::T(const T&)
as a default, but move to T::T(T&&)
otherwise?
Aucun commentaire:
Enregistrer un commentaire