Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ template compilation error: expected primary-expression before ‘>’ token

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.

like image 648
Andrew T Avatar asked Aug 17 '10 18:08

Andrew T


1 Answers

You need to do:

std::cout << pt.template get<std::string>("path");

Use template in the same situation as typename, except for template members instead of types.

(That is, since pt::get is a template member dependent on a template parameter, you need to tell the compiler it's a template.)

like image 102
GManNickG Avatar answered Nov 02 '22 10:11

GManNickG