Hide empty base class for aggregate initialization
Asked Answered
S

4

10

Consider the following code:

struct A
{
    // No data members
    //...
};

template<typename T, size_t N>
struct B : A
{
    T data[N];
}

This is how you have to initialize B: B<int, 3> b = { {}, {1, 2, 3} }; I want to avoid the unnecessary empty {} for the base class. There is a solution proposed by Jarod42 here, however, it doesn't work with elements default initialization: B<int, 3> b = {1, 2, 3}; is fine but B<int, 3> b = {1}; is not: b.data[1] and b.data[2] aren't default initialized to 0, and a compiler error occurs. Is there any way (or there will be with c++20) to "hide" base class from construction?

Scythe answered 27/12, 2019 at 9:27 Comment(4)
Why not to add a constructor template<class... Ts> B(Ts... args) : data{args...} {}?Noctambulous
Why is it a comment? It seems to be working, lolScythe
This is such an obvious solution that I thought you have some reason not to use it. :)Noctambulous
It was too easy xD. If you write it as an answer, i'll accept itScythe
N
6

The easiest solution is to add a variadic constructor:

struct A { };

template<typename T, std::size_t N>
struct B : A {
    template<class... Ts, typename = std::enable_if_t<
        (std::is_convertible_v<Ts, T> && ...)>>
    B(Ts&&... args) : data{std::forward<Ts>(args)...} {}

    T data[N];
};

void foo() {
    B<int, 3> b1 = {1, 2, 3};
    B<int, 3> b2 = {1};
}

If you provide fewer elements in the {...} initializer list than N, the remaining elements in the array data will be value-initialized as by T().

Noctambulous answered 27/12, 2019 at 10:23 Comment(3)
I've just found out why this is different from aggregate initialization. If you consider B<Class, 5> b = {Class()}; Class will be constructed first and then moved, while by using aggregate initialization Class would be in place constructed, no move involvedScythe
@user7769147, good point. You can take std::tuple of arguments and use them to construct objects in-place. But the syntax will be rather cumbersome.Noctambulous
I've randomly found a solution that solves this problem, I'm gonna leave this as accepted answer to thank you for your availability :).Scythe
G
5

Since C++20 you could use designated initializers in aggregate initialization.

B<int, 3> b = { .data {1} }; // initialize b.data with {1}, 
                             // b.data[0] is 1, b.data[1] and b.data[2] would be 0
Glyconeogenesis answered 27/12, 2019 at 9:31 Comment(1)
That's still too verbose for me, that was a minimal example. My array member has a strange name that should be ignored by the userScythe
M
4

Still with constructor, you might do something like:

template<typename T, size_t N>
struct B : A
{
public:
    constexpr B() : data{} {}

    template <typename ... Ts,
              std::enable_if_t<(sizeof...(Ts) != 0 && sizeof...(Ts) < N)
                               || !std::is_same_v<B, std::decay_t<T>>, int> = 0>
    constexpr B(T&& arg, Ts&&... args) : data{std::forward<T>(arg), std::forward<Ts>(args)...}
    {}

    T data[N];
};

Demo

SFINAE is done mainly to avoid to create pseudo copy constructor B(B&).

You would need extra private tag to support B<std::index_sequence<0, 1>, 42> ;-)

Miniature answered 27/12, 2019 at 10:1 Comment(2)
Why do you need ((void)Is, T())...? What if you simply omit it? Won't the remaining elements be value-initialized with T() by default?Noctambulous
@Evg: Indeed, simplified. Was afraid to only default initialize remaining elements instead of value initialize them...Miniature
S
2

I've found another solution that (I don't know how) works perfectly and solves the problem we were discussing under Evg's answer

struct A {};

template<typename T, size_t N>
struct B_data
{
    T data[N];
};

template<typename T, size_t N>
struct B : B_data<T, N>, A
{
    // ...
};
Scythe answered 27/12, 2019 at 11:47 Comment(1)
Interesting solution. But now one has to use this->data or using B_data::data; to access data inside B.Noctambulous

© 2022 - 2024 — McMap. All rights reserved.