[PropertyTree] Using with std algorithms

Hi All Is it possible and practical to use the put and get methods of Boost.PropertyTree with std algorithms? I've written this snippet, which doesn't compile, which I think is because I have not correctly expressed the return type of "put()". Is it possible to do this? Thanks, - Rob. #include <algorithm> #include <vector> #include <boost/range.hpp> #include <boost/bind.hpp> #include <boost/property_tree/ptree.hpp> int main( ) { using boost::property_tree::ptree; std::vector<unsigned> v; ptree tree; for_each( boost::begin( v ), boost::end( v ), bind( static_cast<void (ptree::*)( const std::string &, unsigned )>( & ptree::put ), boost::ref( tree ), "Values.value", _1 ) ); }
g++ ptree.cpp
ptree.cpp: In function ‘int main()’: ptree.cpp:15: error: invalid static_cast from type ‘<unresolved overloaded function type>’ to type ‘boost::property_tree::ptree& (boost::property_tree::basic_ptree<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::*)(const std::string&, unsigned int)’

On 19.05.2011, at 08:56, Robert Jones wrote:
Hi All
Is it possible and practical to use the put and get methods of Boost.PropertyTree with std algorithms?
I've written this snippet, which doesn't compile, which I think is because I have not correctly expressed the return type of "put()". Is it possible to do this?
Yes, the put() overloads all return the affected tree node.
for_each( boost::begin( v ), boost::end( v ), bind( static_cast<void (ptree::*)( const std::string &, unsigned )>( & ptree::put ), boost::ref( tree ), "Values.value", _1 ) );
Try this:
static_cast<ptree& (ptree::*)(...)>
Sebastian

On Thu, May 19, 2011 at 5:24 PM, Sebastian Redl < sebastian.redl@getdesigned.at> wrote:
On 19.05.2011, at 08:56, Robert Jones wrote:
Hi All
Is it possible and practical to use the put and get methods of Boost.PropertyTree with std algorithms?
I've written this snippet, which doesn't compile, which I think is because I have not correctly expressed the return type of "put()". Is it possible to do this?
Yes, the put() overloads all return the affected tree node.
for_each( boost::begin( v ), boost::end( v ), bind( static_cast<void (ptree::*)( const std::string &, unsigned )>( & ptree::put ), boost::ref( tree ), "Values.value", _1 ) );
Try this:
static_cast<ptree& (ptree::*)(...)>
Hi Sebstian Thanks for that, I thought that should work too, but seems there's still an overload problem! #include <algorithm> #include <vector> #include <boost/range.hpp> #include <boost/bind.hpp> #include <boost/property_tree/ptree.hpp> int main( ) { using boost::property_tree::ptree; std::vector<unsigned> v; ptree tree; ptree & ( ptree::*myput )( const std::string &, unsigned ) = & ptree::put; for_each( boost::begin( v ), boost::end( v ), bind( myput, boost::ref( tree ), "Values.value", _1 ) ); }
g++ ptree.cpp
ptree.cpp: In function ‘int main()’: ptree.cpp:14: error: no matches converting function ‘put’ to type ‘struct boost::property_tree::ptree& (struct boost::property_tree::basic_ptree<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::*)(const struct std::string&, unsigned int)’ /usr/include/boost/property_tree/ptree.hpp:446: error: candidates are: template<class Type, class Translator> boost::property_tree::basic_ptree<K, D, C>& boost::property_tree::basic_ptree::put(const typename boost::property_tree::path_of<Key>::type&, const Type&, Translator) [with Type = Type, Translator = Translator, Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Data = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, KeyCompare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >] /usr/include/boost/property_tree/ptree.hpp:455: error: template<class Type> boost::property_tree::basic_ptree<K, D, C>& boost::property_tree::basic_ptree::put(const typename boost::property_tree::path_of<Key>::type&, const Type&) [with Type = Type, Key = std::basic_string<char, std::char_traits<char>, std::allocator<char>
, Data = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, KeyCompare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]
Any ideas? Thx - Rob.

On 19.05.2011, at 10:52, Robert Jones wrote:
On Thu, May 19, 2011 at 5:24 PM, Sebastian Redl <sebastian.redl@getdesigned.at> wrote:
On 19.05.2011, at 08:56, Robert Jones wrote:
Hi All
Is it possible and practical to use the put and get methods of Boost.PropertyTree with std algorithms?
I've written this snippet, which doesn't compile, which I think is because I have not correctly expressed the return type of "put()". Is it possible to do this?
Yes, the put() overloads all return the affected tree node.
for_each( boost::begin( v ), boost::end( v ), bind( static_cast<void (ptree::*)( const std::string &, unsigned )>( & ptree::put ), boost::ref( tree ), "Values.value", _1 ) );
Try this:
static_cast<ptree& (ptree::*)(...)>
Hi Sebstian
Thanks for that, I thought that should work too, but seems there's still an overload problem!
#include <algorithm> #include <vector> #include <boost/range.hpp> #include <boost/bind.hpp> #include <boost/property_tree/ptree.hpp>
int main( ) { using boost::property_tree::ptree;
std::vector<unsigned> v; ptree tree;
ptree & ( ptree::*myput )( const std::string &, unsigned ) = & ptree::put;
The first argument should be a ptree::path_type (= string_path<char>), not std::string. Sebastian

On Thu, May 19, 2011 at 6:00 PM, Sebastian Redl < sebastian.redl@getdesigned.at> wrote:
On 19.05.2011, at 10:52, Robert Jones wrote:
On Thu, May 19, 2011 at 5:24 PM, Sebastian Redl < sebastian.redl@getdesigned.at> wrote:
On 19.05.2011, at 08:56, Robert Jones wrote:
Hi All
Is it possible and practical to use the put and get methods of Boost.PropertyTree with std algorithms?
I've written this snippet, which doesn't compile, which I think is because I have not correctly expressed the return type of "put()". Is it possible to do this?
Yes, the put() overloads all return the affected tree node.
for_each( boost::begin( v ), boost::end( v ), bind( static_cast<void (ptree::*)( const std::string &, unsigned )>( & ptree::put ), boost::ref( tree ), "Values.value", _1 ) );
Try this:
static_cast<ptree& (ptree::*)(...)>
Hi Sebstian
Thanks for that, I thought that should work too, but seems there's still an overload problem!
#include <algorithm> #include <vector> #include <boost/range.hpp> #include <boost/bind.hpp> #include <boost/property_tree/ptree.hpp>
int main( ) { using boost::property_tree::ptree;
std::vector<unsigned> v; ptree tree;
ptree & ( ptree::*myput )( const std::string &, unsigned ) = & ptree::put;
The first argument should be a ptree::path_type (= string_path<char>), not std::string.
Hi Sebastian YES! At last, it works. I had to change the last parameter to a const reference too, but your strategy of correctly matching the exact signature is the way to go. Final code... int main( ) { using boost::property_tree::ptree; std::vector<unsigned> v; ptree tree; ptree & ( ptree::*myput )( const ptree::path_type &, const unsigned & ) = & ptree::put; for_each( boost::begin( v ), boost::end( v ), bind( myput, boost::ref( tree ), "Values.value", _1 ) ); } Many Thanks - Rob.
participants (2)
-
Robert Jones
-
Sebastian Redl