Quantcast

Using assign::map_list_of for complex types

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Using assign::map_list_of for complex types

Hughes, James
Hello all,

I've been trying to create an initiliser for the following (paraphrased)

struct data
{
Char *str;
Vector<int> stuff;
};


map<int, struct data> mapstuff = map_list_of
(1, ("hello", list_of (3) (4) (2)),
(3, ("goodbye", list_of (1) (3) );

Etc etc

However, this doesn't work, with a very complex compile error from
assign. I've tried also...

map<int, struct data> mapstuff = map_list_of
(1, {"hello", list_of (3) (4) (2) } ),   // note curly braces
(3, {"goodbye", list_of (1) (3) ) } );

But that doesn't work either.

Is it actually possible to do something like this, and if so, how?

James

This message (including any attachments) contains confidential
and/or proprietary information intended only for the addressee.  
Any unauthorized disclosure, copying, distribution or reliance on
the contents of this information is strictly prohibited and may
constitute a violation of law.  If you are not the intended
recipient, please notify the sender immediately by responding to
this e-mail, and delete the message from your system.  If you
have any questions about this e-mail please notify the sender
immediately.
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using assign::map_list_of for complex types

Thorsten Ottosen-3
Hughes, James wrote:
> Hello all,
>
> I've been trying to create an initiliser for the following (paraphrased)
>
> struct data
> {
> Char *str;

const char*?

> Vector<int> stuff;
> };
>
>
> map<int, struct data> mapstuff = map_list_of
> (1, ("hello", list_of (3) (4) (2)),
> (3, ("goodbye", list_of (1) (3) );
>
> Etc etc
>
> However, this doesn't work, with a very complex compile error from
> assign. I've tried also...
>
> map<int, struct data> mapstuff = map_list_of
> (1, {"hello", list_of (3) (4) (2) } ),   // note curly braces
> (3, {"goodbye", list_of (1) (3) ) } );
>
> But that doesn't work either.
>
> Is it actually possible to do something like this, and if so, how?

I suspect that you have to provide a constructor for your data class:

template< class Range >
data( const char* str, const Range& r )
: str(str), stuff(boost::begin(r),boost::end(r))
{ }

-Thorsten
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using assign::map_list_of for complex types

Hughes, James
In reply to this post by Hughes, James

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of
> Thorsten Ottosen
> Sent: 15 November 2006 12:00
> To: [hidden email]
> Subject: Re: [Boost-users] Using assign::map_list_of for complex types
>
> Hughes, James wrote:
> > Hello all,
> >
> > I've been trying to create an initiliser for the following
> > (paraphrased)
> >
> > struct data
> > {
> > Char *str;
>
> const char*?

Typo when copying from Linux box to email box - soz!

>
> > Vector<int> stuff;
> > };
> >
> >
> > map<int, struct data> mapstuff = map_list_of (1, ("hello",
> list_of (3)
> > (4) (2)), (3, ("goodbye", list_of (1) (3) );
> >
> > Etc etc
> >
> > However, this doesn't work, with a very complex compile error from
> > assign. I've tried also...
> >
> > map<int, struct data> mapstuff = map_list_of
> > (1, {"hello", list_of (3) (4) (2) } ),   // note curly braces
> > (3, {"goodbye", list_of (1) (3) ) } );
> >
> > But that doesn't work either.
> >
> > Is it actually possible to do something like this, and if so, how?
>
> I suspect that you have to provide a constructor for your data class:
>
> template< class Range >
> data( const char* str, const Range& r )
> : str(str), stuff(boost::begin(r),boost::end(r))
> { }
>

I did something along these lines but still got a load of compile
faults. Will post a more accurate code snippet when time allows, but
have a deadline end of today!!


> -Thorsten
> _______________________________________________
> Boost-users mailing list
> [hidden email]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

This message (including any attachments) contains confidential
and/or proprietary information intended only for the addressee.  
Any unauthorized disclosure, copying, distribution or reliance on
the contents of this information is strictly prohibited and may
constitute a violation of law.  If you are not the intended
recipient, please notify the sender immediately by responding to
this e-mail, and delete the message from your system.  If you
have any questions about this e-mail please notify the sender
immediately.
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using assign::map_list_of for complex types

Thorsten Ottosen-3
Hughes, James wrote:

>>>map<int, struct data> mapstuff = map_list_of
>>>(1, {"hello", list_of (3) (4) (2) } ),   // note curly braces
>>>(3, {"goodbye", list_of (1) (3) ) } );
>>>
>>>But that doesn't work either.
>>>
>>>Is it actually possible to do something like this, and if so, how?
>>
>>I suspect that you have to provide a constructor for your data class:
>>
>>template< class Range >
>>data( const char* str, const Range& r )
>>: str(str), stuff(boost::begin(r),boost::end(r))
>>{ }
>>
>
>
> I did something along these lines but still got a load of compile
> faults. Will post a more accurate code snippet when time allows, but
> have a deadline end of today!!

You also need to call the constructor when you call map_list_of():

   map_list_of(1, data( "hello", list_of(3)(4)(4) ) );

-Thorsten
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using assign::map_list_of for complex types

Hughes, James
In reply to this post by Hughes, James
Clip..

> >>{ }
> >>
> >
> >
> > I did something along these lines but still got a load of compile
> > faults. Will post a more accurate code snippet when time
> allows, but
> > have a deadline end of today!!
>
> You also need to call the constructor when you call map_list_of():
>
>    map_list_of(1, data( "hello", list_of(3)(4)(4) ) );
>
> -Thorsten
> ____


Thanks Thorsten, that did the trick. I had actually tried something
along those line when trying to figure it out myself, but didn't know
how to handle the range stuff you put in your example. Thanks again.

As a sort of aside, this example would make a good addition to the
documentation, for those coming to C++ from C where this sort of nested
structure initialisation is common (well, in my world it is!).

James

___________________________________________
> Boost-users mailing list
> [hidden email]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

This message (including any attachments) contains confidential
and/or proprietary information intended only for the addressee.  
Any unauthorized disclosure, copying, distribution or reliance on
the contents of this information is strictly prohibited and may
constitute a violation of law.  If you are not the intended
recipient, please notify the sender immediately by responding to
this e-mail, and delete the message from your system.  If you
have any questions about this e-mail please notify the sender
immediately.
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using assign::map_list_of for complex types

Thorsten Ottosen-3
Hughes, James wrote:

> Clip..
>
>>>>{ }
>>>>
>>>
>>>
>>>I did something along these lines but still got a load of compile
>>>faults. Will post a more accurate code snippet when time
>>
>>allows, but
>>
>>>have a deadline end of today!!
>>
>>You also need to call the constructor when you call map_list_of():
>>
>>   map_list_of(1, data( "hello", list_of(3)(4)(4) ) );
>>
>>-Thorsten
>>____
>
>
>
> Thanks Thorsten, that did the trick. I had actually tried something
> along those line when trying to figure it out myself, but didn't know
> how to handle the range stuff you put in your example. Thanks again.

You're welcome. It will work if you have a normal vector<int> there too,
although that is slower.

> As a sort of aside, this example would make a good addition to the
> documentation, for those coming to C++ from C where this sort of nested
> structure initialisation is common (well, in my world it is!).

I'll leave that as a todo. It is already documented that the result of
list_of() is a Range.

-Thorsten
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Loading...