yves Baumes
yves Baumes

Reputation: 9036

How can I push an aggregate initialiser list into a vector?

I am trying to write something like that:

vector<iovec> iovecs;
iovec io = {&foo, sizeof(foo)};
iovevs.push_base(io);

which is ok for the compiler.

Now I am trying to transform it into something more concise, like that:

 vector<iovec> iovecs;
 iovecs.push_back({&foo, sizeof(foo)});

But I am rejected with error msg like:

warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x

I do not want to enable c++0x. Still I want to be able to uses the aggregates init style. Is there any way to do that?

Upvotes: 0

Views: 145

Answers (1)

Filip Ros&#233;en
Filip Ros&#233;en

Reputation: 63862

That usage of braced-initializer in terms of uniform-initialization is a feature introduced with C++11.

If you don't want to enable such compiler support you are quite naturally forced to work with what you have in the earlier standard(s); which, as you have already found out, doesn't include uniform-initialization.


A simple "hack" that might ease your pain would be something as the below:

template<typename T, typename U>
iovec make_iovec (T const& a, U const& b) {
 iovec  ret = {a, b};
 return ret;
}

iovecs.push_back (make_iovec (&foo, sizeof(foo)));

Upvotes: 3

Related Questions