This code compiles and works as expected (it throws at runtime, but never mind):
#include <iostream>
#include <boost/property_tree/ptree.hpp>
void foo(boost::property_tree::ptree &pt)
{
std::cout << pt.get<std::string>("path"); // <---
}
int main()
{
boost::property_tree::ptree pt;
foo(pt);
return 0;
}
But as soon as I add templates and change the foo
prototype into
template<class ptree>
void foo(ptree &pt)
I get an error in GCC:
test_ptree.cpp: In function ‘void foo(ptree&)’:
test_ptree.cpp:7: error: expected primary-expression before ‘>’ token
but no errors with MSVC++! The error is in the marked line <---
. And again, if I change the problem line into
--- std::cout << pt.get<std::string>("path"); // <---
+++ std::cout << pt.get("path", "default value");
the error disappears (the problem is in explicit <std::string>
).
Boost.PropertyTree requires Boost >= 1.41. Please help me to understand and fix this error.
See Templates: template function not playing well with class’s template member function — a similar popular question containing other good answers and explanations.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…