Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

brace initialization of subclasse of array

42 views
Skip to first unread message

Ralf Goertz

unread,
Sep 14, 2017, 4:19:15 AM9/14/17
to
Hi,

why does brace initialization of a subclass of array fail and how do I
fix this?


#include <array>

//typedef std::array<double,3> Space; //with this line it works
struct Space : public std::array<double,3>{};

int main() {
Space x={{1,2,3}}; //fails when Space is subclass of array
return 0;
}

Alf P. Steinbach

unread,
Sep 14, 2017, 4:48:41 AM9/14/17
to
It's no longer an aggregate (simple class with no bases or user-provided
constructors). List-initialization reduces to aggregate initialization
when the class is an aggregate. Otherwise various special cases are
considered, e.g. initializing a `std::initializer_list`, and in your
case constructors are considered, and none were found.

You can simply provide a constructor that takes a `std::initializer_list`.

`std::array` stems from Boost, from the time before
`std::initializer_list`, and was designed for old C++ and C aggregate
initialization.


Cheers & hth.,

- Alf

Ralf Goertz

unread,
Sep 14, 2017, 6:22:05 AM9/14/17
to
Am Thu, 14 Sep 2017 10:48:19 +0200
schrieb "Alf P. Steinbach" <alf.p.stein...@gmail.com>:

> On 9/14/2017 10:18 AM, Ralf Goertz wrote:
> > Hi,
> >
> > why does brace initialization of a subclass of array fail and how
> > do I fix this?
> >
> >
> > #include <array>
> >
> > //typedef std::array<double,3> Space; //with this line it works
> > struct Space : public std::array<double,3>{};
> >
> > int main() {
> > Space x={{1,2,3}}; //fails when Space is subclass of array
> > return 0;
> > }
> >
>
> It's no longer an aggregate (simple class with no bases or
> user-provided constructors). List-initialization reduces to aggregate
> initialization when the class is an aggregate.

Hm, you mean to say it works with array because it doesn't have a
user-provided constructor so it reduces to aggregate initialization?
Which then fails in a subclass because it is no longer aggregate? But
the above program also fails to compile when I substitute array with
vector (but still works with the typedef). And vector does have
non-default constructors hence it is not aggregate. So why doesn't a
subclass of vector inherit its list-initialization constructors?

Alf P. Steinbach

unread,
Sep 14, 2017, 10:15:00 AM9/14/17
to
If you want constructor inheritance then you have to specify that
explicitly, e.g. (off the cuff)

struct Foo
: std::vector<int>
{
using std::vector<int>::vector<int>; // Inherit constrcts
};

Ralf Goertz

unread,
Sep 14, 2017, 10:43:35 AM9/14/17
to
Am Thu, 14 Sep 2017 16:14:31 +0200
schrieb "Alf P. Steinbach" <alf.p.stein...@gmail.com>:


> If you want constructor inheritance then you have to specify that
> explicitly, e.g. (off the cuff)

That cuff needs a link ;-)

> struct Foo
> : std::vector<int>
> {
> using std::vector<int>::vector<int>; // Inherit constrcts

error: a template-id may not appear in a using-declaration

> };

I don't seem to be able to fix that though. :-(

Alf P. Steinbach

unread,
Sep 14, 2017, 12:14:42 PM9/14/17
to
Oh sorry.

#include <vector>

struct Foo
: std::vector<int>
{
using std::vector<int>::vector;
};

auto main()
-> int
{
Foo const v{ 1, 2, 3 };
}

There.

Another way is to define a name for the base class.

asetof...@gmail.com

unread,
Sep 14, 2017, 3:31:17 PM9/14/17
to
Using
std::ftrytr:: Etc
As the line you wrote
it is for me horrible
There would be some other way to access in namespace

Öö Tiib

unread,
Sep 16, 2017, 5:03:32 AM9/16/17
to
On Thursday, 14 September 2017 13:22:05 UTC+3, Ralf Goertz wrote:
> So why doesn't a
> subclass of vector inherit its list-initialization constructors?

Because language rules (IMHO fortunately) do not suggest anywhere that
subclass should somehow automatically inherit all constructors.
0 new messages