Re: [boost] [Spirit-general] Spirit Questions

Thanks, I saw this a long time ago at codeproject also. Is there any real hope in the future that boost::any will take advantage of this implementation? This is related to my original question. While using hold_any, I run into this problem when trying to use BOOST_SPIRIT_DEBUG with a vector of hold_any. binary '>>' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion) 1> with 1> [ 1> _Ty=boost::spirit::hold_any 1> ] The code I added was as follows std::istream& operator>> (std::istream& i, vector<hold_any>& obj) { return i;//obj.table->stream_in(i, &obj.object); } std::ostream& operator<< (std::ostream& o, vector<hold_any> const& obj) { o << "["; for(size_t i = 0; i < obj.size(); i++) { if(i > 0) o << ","; o << obj[i]; } o << "]"; return o; } -----Original Message----- From: spirit-general-bounces@lists.sourceforge.net [mailto:spirit-general-bounces@lists.sourceforge.net] On Behalf Of Hartmut Kaiser Sent: Friday, July 13, 2007 10:01 AM To: 'Spirit General Mailing List' Subject: Re: [Spirit-general] Spirit Questions Joel de Guzman wrote:
*Question #3 of 3: How do I debug using '#define BOOST_SPIRIT_DEBUG' when using closures with a member of any?*
Any needs stream operators which it doesn't have. I know this may be an idiot question but I am asking it anyway because I tried the following 2 and could get neither to work. Any assistance would be appreciated.
Well, this is outside the scope of Spirit. I think the proper forum in this case is, back to the boost list -- just for this particular case. If I were you, I'd use a boost.variant instead and use its visitation mechanism to print its contents. There's really no fool proof way to print a boost any's contents.
FWIW, Spirit2 contains a hold_any class which is: A) faster than Boost.Any, B) supports input/output streaming as long as the elements stored in the hold_any do support streaming as well, C) is otherwise 100% compatible to Boost.Any. Regards Hartmut (BTW, it's here: http://tinyurl.com/242kyt) ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/ _______________________________________________ Spirit-general mailing list Spirit-general@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/spirit-general

Jarrad,
Thanks, I saw this a long time ago at codeproject also. Is there any real hope in the future that boost::any will take advantage of this implementation?
Only, if somebody cares to push it through the review process (writes documentation, etc.). Currently it's just an internal implementation detail in Spirit2.
This is related to my original question. While using hold_any, I run into this problem when trying to use BOOST_SPIRIT_DEBUG with a vector of hold_any.
binary '>>' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion) 1> with 1> [ 1> _Ty=boost::spirit::hold_any 1> ]
The code I added was as follows
std::istream& operator>> (std::istream& i, vector<hold_any>& obj) { return i;//obj.table->stream_in(i, &obj.object); }
std::ostream& operator<< (std::ostream& o, vector<hold_any> const& obj) { o << "["; for(size_t i = 0; i < obj.size(); i++) { if(i > 0) o << ","; o << obj[i]; } o << "]"; return o; }
Seems to be fine this way. Hold_any supports streaming only if the contained datatype is streamable. Regards Hartmut

Hartmut Kaiser wrote: ...
Seems to be fine this way. Hold_any supports streaming only if the contained datatype is streamable.
Are you sure that hold_any can be used with a non-streamable type at all? Also, in assign<T>, are you sure that overwriting the old object contents with placement new without destroying the object first is a good idea? Looks like repeated a = std::string( "..." ) calls will leak the heap-allocated string contents on each assignment.

Hartmut Kaiser wrote: ...
Seems to be fine this way. Hold_any supports streaming only if the contained datatype is streamable.
Are you sure that hold_any can be used with a non-streamable type at all?
Let me re-phrase it (sorry for beeing inconcise) : hold_any can be used (compiled) only, if the type contained is streamable. So yes, you're right, hold_any can't be used with non-streamable types.
Also, in assign<T>, are you sure that overwriting the old object contents with placement new without destroying the object first is a good idea? Looks like repeated a = std::string( "..." ) calls will leak the heap-allocated string contents on each assignment.
Good catch! Thanks, will fix this. Regards Hartmut
participants (3)
-
Hartmut Kaiser
-
Jarrad Waterloo
-
Peter Dimov