"expected initializer before" <token "with built-in template function in the global namespace

I am trying to compile some code, in one of my headers I have the following function in the global namespace:

 template <class T> inline T to_type<T> (const std::string& string) { std::stringstream ss(string); T value; ss >> value; return value; } 

But for some reason this causes the g ++ expected initializer before '<' token error (I changed one of the quotes to resolve the conflict with SO formatting)

I do not understand this error. Why is to_type not a valid initializer? This is the first time this symbol has been used. How to fix this fragment?

+4
source share
1 answer

The correct syntax

 template <class T> inline T to_type(const std::string& string) { std::stringstream ss(string); T value; ss >> value; return value; } 

(note no <T> after to_type ).

<> are placed only after the name of the declared function (or class) when declaring specialization, and not when declaring a base template.

+4
source

Source: https://habr.com/ru/post/1416323/


All Articles