Baz
Baz

Reputation: 13123

Initializing std::vector of structs

I wish to add a bunch of objects of this type to a std::vector.

typedef struct 
{
    int handle;
} Handle;

Handle is defined within a C API header which I am unable to change.

I'm doing this at the moment but am wondering if it can be done as a one-liner.

Handle handle1 = {12};
Handle handle2 = {13};
std::vector<Handle> handles = boost::assign::list_of(handle1)(handle2);

I using a C++98 compiler.

Upvotes: 1

Views: 211

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

Just write a make_handle function:

Handle make_handle(int handle) {
    Handle ret = { handle };
    return ret;
}

Upvotes: 1

Related Questions