Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2019-12-04 23:37:04

> - everything should ideally be constexpr. The problem here is that
> constexpr requires initializing all elements, and this will heavily
> penalize runtime uses of f.ex. fixed_string<512>. On compilers with
> __builtin_is_constant_evaluated (gcc 9, clang 9) we should use that;
> otherwise, it might be worth it to create a specialization for N < some
> suitable upper limit.

Wait, I have an idea.

template<std::size_t N> struct fixed_string

    std::size_t size_ = 0;

        char first_ = 0;
        char data_[ N+1 ];


    fixed_string() = default;

Now default initialization sets just the first char to zero, as we want, and
value initialization initializes all data_ to zero and is constexpr.

auto f1()
    fixed_string<24> s;
    return s;

auto f2()
    constexpr fixed_string<24> s{};
    return s;

I don't know if that's defined behavior or not, but if it isn't, it better

Boost list run by bdawes at, gregod at, cpdaniel at, john at