Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid {} when using aggregate initialization with empty base class

C++17's aggregate initialization for base class is awesome, but it is verbose when the base is only there to provide some functions (so no data members).

Here is minimal example:

#include <cstddef>
struct base_pod
{
    // functions like friend compare operator
};
template<typename T, std::size_t N>
struct der_pod : public base_pod
{
    T k[N];
};

int main()
{
    der_pod<int, 2> dp {{}, {3, 3} };
}

As the example above shows, I have to provide empty {}, otherwise compile error will occur. live demo. If I omit it:

prog.cc:15:28: error: initializer for aggregate with no elements requires explicit braces
        der_pod<int, 2> dp{3, 3};
                           ^
prog.cc:15:31: warning: suggest braces around initialization of subobject [-Wmissing-braces]
        der_pod<int, 2> dp{3, 3};
                              ^
                              {}
1 warning and 1 error generated.

Any workaround or pre-C++17 way?

like image 859
Chen Li Avatar asked Apr 09 '19 12:04

Chen Li


People also ask

What is brace initialization?

If a type has a default constructor, either implicitly or explicitly declared, you can use brace initialization with empty braces to invoke it. For example, the following class may be initialized by using both empty and non-empty brace initialization: C++ Copy.

What is aggregate initialization in C++?

An aggregate is just what it sounds like: a bunch of things clumped together. This definition includes aggregates of mixed types, like structs and classes. An array is an aggregate of a single type. Initializing aggregates can be error-prone and tedious. C++ aggregate initialization makes it much safer.

What is uniform initialization in C++?

Uniform initialization is a feature in C++ 11 that allows the usage of a consistent syntax to initialize variables and objects ranging from primitive type to aggregates. In other words, it introduces brace-initialization that uses braces ({}) to enclose initializer values.

What is aggregate type C++?

Formal definition from the C++ standard (C++03 8.5. 1 §1): An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).


1 Answers

You can still provide constructor, for example:

template <typename T, std::size_t N> using always_t = T;

struct base_pod
{
    // functions like friend compare operator
};
template<typename T, typename Seq> struct der_pod_impl;

template<typename T, std::size_t ... Is>
struct der_pod_impl<T, std::index_sequence<Is...>> : base_pod
{
    der_pod_impl(always_t<T, Is>... args) : k{args...} {}

    T k[sizeof...(Is)];
};

template<typename T, std::size_t N>
using der_pod = der_pod_impl<T, std::make_index_sequence<N>>;

Demo

like image 147
Jarod42 Avatar answered Sep 28 '22 10:09

Jarod42