
I was working on some code where I got some parameters as pairs of strings [name, value]. In my code I needed to convert those values to int's, doubles, etc (i.e. deserialize). And I wanted to convert them in bunch. I thought it was a great job for lexical_cast and tuple::tie. The only thing I missed was something like for_each for tuples. Is there going to be such a mechanism? Here is an example: template<class T, class F> inline void for_each(boost::tuples::cons<T, boost::tuples::null_type> const& tuple, F f) { f(tuple.get_head()); } template<class T, class U, class F> inline void for_each(boost::tuples::cons<T, U> const& tuple, F f) { f(tuple.get_head()); for_each(tuple.get_tail(), f); } struct printer { template<class T> void operator()(T& t) const { cout << typeid(t).name() << " (" << t << ")\n"; } }; typedef std::map<std::string, std::string> parameters; struct lexical_caster { parameters const* p; char const* name; template<class T> void operator()(T& t) { parameters::key_type const k(name); parameters::const_iterator i(p->find(k)); if(p->end() == i) throw std::runtime_error("a parameter is not found"); t = boost::lexical_cast<T>(i->second); name += 1 + k.size(); } }; int main() { bool b(1); char c('a'); int i(12345); double d(3.1428); for_each(boost::tie(b, c, i, d), printer()); parameters p; p["bool"] = "0"; p["char"] = "b"; p["int"] = "67890"; p["double"] = "1.6"; lexical_caster l = { &p, "bool\0char\0int\0double\0" }; for_each(boost::tie(b, c, i, d), l); for_each(boost::tie(b, c, i, d), printer()); } -- Maxim Yegorushkin

"Maxim Yegorushkin" <e-maxim@yandex.ru> writes:
I was working on some code where I got some parameters as pairs of strings [name, value]. In my code I needed to convert those values to int's, doubles, etc (i.e. deserialize). And I wanted to convert them in bunch. I thought it was a great job for lexical_cast and tuple::tie.
The only thing I missed was something like for_each for tuples. Is there going to be such a mechanism?
Yeah. After this release we're replacing Boost tuples with the fusion library (see boost/spirit/fusion), which has for_each and much much more. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

Thank you for mentioning this code. I have looked for a tuple "for_each" as well. I looked at the fusion code, and I noticed there is quite a bit of similiarity in functions to mpl sequences. Does this mean that tuples will work with mpl sequences, so that a tuple implements all the required interfaces for mpl? Thanks David Abrahams wrote:
"Maxim Yegorushkin" <e-maxim@yandex.ru> writes:
I was working on some code where I got some parameters as pairs of strings [name, value]. In my code I needed to convert those values to int's, doubles, etc (i.e. deserialize). And I wanted to convert them in bunch. I thought it was a great job for lexical_cast and tuple::tie.
The only thing I missed was something like for_each for tuples. Is there going to be such a mechanism?
Yeah. After this release we're replacing Boost tuples with the fusion library (see boost/spirit/fusion), which has for_each and much much more.

Tito Villalobos <tvillalobos@wowway.com> writes:
Thank you for mentioning this code. I have looked for a tuple "for_each" as well. I looked at the fusion code, and I noticed there is quite a bit of similiarity in functions to mpl sequences. Does this mean that tuples will work with mpl sequences, so that a tuple implements all the required interfaces for mpl?
Precisely. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
participants (3)
-
David Abrahams
-
Maxim Yegorushkin
-
Tito Villalobos