Search code examples
c++templatesboostcompiler-errorsboost-propertytree

Typedef'ing basic_ptree from Boost


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!


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.