gnzlbg / static_vector

A dynamically-resizable vector with fixed capacity and embedded storage
https://gnzlbg.github.io/static_vector
167 stars 21 forks source link

Tags are better than static member functions #14

Closed gnzlbg closed 8 years ago

gnzlbg commented 8 years ago

The section on default initialization:

Default initialization (possible future Extension)

The size-modifying operations of the inline_vector that do not require a value also have the following analogous member functions that perform default initialization instead of value initialization:

template <typename Value, std::size_t Capacity>
struct inline_vector {
// ...
    static constexpr inline_vector default_initialized(size_t n);
    constexpr void resize_default_initialized(size_type sz);
    constexpr void resize_unchecked_default_initialized(size_type sz);
};

Alternatively, tag dispatching could be used.

prefers static member functions to tags, but tags are objectively better:

Using tags:

optional<inline_vector<T, Capacity>> ov {
    std::in_place, std::default_initialized, N
};

the inline_vector can be constructed in place without involving a copy or move, but using a static member function:

optional<inline_vector<T, Capacity>> ov {
    std::in_place, inline_vector<T, Capacity>::default_initialized(N)
};

will necessarily incur the cost of a move operation (which in particular for inline_vector will be often as expensive as a copy).

Thanks to @akrzemi1 for the discussion! (add to acknowledgements)

gnzlbg commented 8 years ago

Closed by https://github.com/gnzlbg/inline_vector/commit/4d5aea12d87c49122f99c021f455fa17086f36ce .