jeudi 30 août 2018

uninitialized_fill_n not forwarding arguments

I am implementing a container, that following the example of std::vector, has a private unitialize_construct.

template<class... Args>
auto uninitialized_construct(Args&&... args){
    typename array::element_ptr cur = this->data();
    for(size_type n = this->num_elements(); n > 0; --n, ++cur)
            alloc_traits::construct(allocator_, std::addressof(*cur), std::forward<Args>(args)...);
    }catch(...){destroy(cur); throw;}
}

I though that, for generic programming purposes, it would be better to use an algorithm instead of a raw loop. It turns out that the standard has std::unitialized_fill_n that does a placement new and effectively it is very similar.

https://en.cppreference.com/w/cpp/memory/uninitialized_fill_n

However, there is a big problem, std::uninitialized_fill_n doesn't have a variadic argument, and therefore I cannot forward args....

a) Why is it so? Is there another variant of uninitialized_fill_n that can forward it construction arguments?

b) If not, am I supposed to call unitialized_fill_n(data, size, value_type(std::forward<Args>(args)...))?

To reiterate, I was expecting to be able to say unitialized_fill_n(data, size, std::forward<Args>(args)...).

Aucun commentaire:

Enregistrer un commentaire