Question

I'm using Boost.PropertyTree for a project and I want to use user-defined types for Key and Data instead of the std::string that Boost uses in the ptree typedef.

However when I typedef basic_ptree myself I get the following compiler errors:

1>  main.cpp
1>c:\boost_1_49_0\boost\property_tree\ptree.hpp(82): error C2027: use of undefined type 'boost::property_tree::path_of<Key>'
1>          with
1>          [
1>              Key=int
1>          ]
1>          c:\users\mathias\documents\visual studio 2012\projects\testappern\testappern\main.cpp(10) : see reference to class template instantiation 'boost::property_tree::basic_ptree<Key,Data>' being compiled
1>          with
1>          [
1>              Key=int,
1>              Data=int
1>          ]
1>c:\users\mathias\documents\visual studio 2012\projects\testappern\testappern\main.cpp(13): error C2664: 'boost::property_tree::basic_ptree<Key,Data>::add_child' : cannot convert parameter 1 from 'int' to 'const boost::type &'
1>          with
1>          [
1>              Key=int,
1>              Data=int
1>          ]
1>          Reason: cannot convert from 'int' to 'const boost::type'
1>          The target type has no constructors
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The following snippet shows and example how I typedef basic_tree and get the compiler errors:

#include <iostream>
#include <boost\property_tree\ptree.hpp>

using namespace boost::property_tree;

typedef basic_ptree<int, int> IntTree;

int main(int argc, char* argv[])
{
    IntTree tree;
    IntTree child;
    int index = 42;
    tree.add_child(index, child);

    return 0;
}

So my question is, how do I typedef it correctly? If it's any interest I'm running MSVC 2012.

Thanks in advance!

Was it helpful?

Solution

According to the contents of boost/property_tree/ptree_fwd.hpp, if you want to use a custom type as a key, you should follow this:

/// If you want to use a custom key type, specialize this struct for it
/// and give it a 'type' typedef that specifies your path type. The path
/// type must conform to the Path concept described in the documentation.
/// This is already specialized for std::basic_string.

So you can not simply use int. Refer to the documentation for more information, as the code says.

OTHER TIPS

Here is an example of basic_ptree <int, int>

//path class must conform Path concept
template<> class boost::property_tree::path_of < int >
{
public:
    typedef int key_type;
    typedef boost::property_tree::path_of < int > type;
    boost::property_tree::path_of<int>(vector<int>& vals)
    {
        std::transform(vals.begin(), vals.end(), back_inserter(_impl),  
            [&](int v) -> int{ return v; });
    }
    key_type reduce()   
    {
        key_type res = *_impl.begin();
        _impl.pop_front();
        return res;
    }
    bool empty() const
    {
        return _impl.empty();
    }
    bool single() const
    {
        return _impl.size() == 1;
    }
    std::string dump() const
    {
        std::string res;
        std::for_each(_impl.begin(),
        _impl.end(), [&res](key_type k) -> void
        {
            res.append(".");
            res.append(boost::lexical_cast<std::string>(k));
        });
        return res;
    }
    private:
         deque<key_type> _impl;
};
//usage:
typedef boost::property_tree::basic_ptree < int, int> custom_int_tree_t
//or 
typedef boost::property_tree::basic_ptree < int, void*> custom_int_tree2_t
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top