initializer lists and user-defined containers

J

jfindlay

Is it possible to populate a container with an initializer list? I am
not too particular about how hackish or ugly any suggestions might be,
I'm simply interested in investigating possible elegancies (or
perversities) for an initializer list.

const container<double> array = mitigating_struct<double>() = {pi, e,
phi, gamma, 0.0042};
// container is magically filled with initalizer list members

Of course, something without an explicit temporary would be more ideal,
but perhaps beyond the realm of C++.


Justin
 
G

Gianni Mariani

Is it possible to populate a container with an initializer list? I am
not too particular about how hackish or ugly any suggestions might be,
I'm simply interested in investigating possible elegancies (or
perversities) for an initializer list.

const container<double> array = mitigating_struct<double>() = {pi, e,
phi, gamma, 0.0042};
// container is magically filled with initalizer list members

Of course, something without an explicit temporary would be more ideal,
but perhaps beyond the realm of C++.

It sounds like this is probably not what you want but it should be
interesting anyway.

http://bdsoft.com/tools/initutil.html
 
I

Ioannis Vranos

Is it possible to populate a container with an initializer list? I am
not too particular about how hackish or ugly any suggestions might be,
I'm simply interested in investigating possible elegancies (or
perversities) for an initializer list.

const container<double> array = mitigating_struct<double>() = {pi, e,
phi, gamma, 0.0042};
// container is magically filled with initalizer list members

Of course, something without an explicit temporary would be more ideal,
but perhaps beyond the realm of C++.


You may use a built in array:


int array[]={1,2,3,4,5,6,7};

vector<int> v(array, array+7);



or using a temp:


vector<int> v;

{
int temp[]={1,2,3,4,5,6,7};

v.assign(temp, temp+7);
}
 
T

Thorsten Ottosen

| Is it possible to populate a container with an initializer list? I am
| not too particular about how hackish or ugly any suggestions might be,
| I'm simply interested in investigating possible elegancies (or
| perversities) for an initializer list.
|
| const container<double> array = mitigating_struct<double>() = {pi, e,
| phi, gamma, 0.0042};
| // container is magically filled with initalizer list members
|
| Of course, something without an explicit temporary would be more ideal,
| but perhaps beyond the realm of C++.

maybe boost.assign will be just what you want (see www.boost.org) It will be
in the next boost release, but you can
already get it from the main cvs if you want.

Shortly put (and among other ways), it allows you to say

const container<double> array = list_of<double>( pi )( e )( phi )( gamma )(
0.00042 );

br

Thorsten
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top