如何在模板函数中检索 mapbox::util::variant 值

How to retrieve mapbox::util::variant value in a templated function

使用 mapbox::variant (https://github.com/mapbox/variant/blob/master/include/mapbox/variant.hpp),我可以执行以下操作:

using variant = mapbox::util::variant<Args...>;
variant<std::string> v;
// do something with v
...
// Get string from v:
std::string s = v.get<std::string>();

但是当我尝试通过模板函数实现它时,出现编译错误:

template <typename T>
T getValue()
{
    variant<T> value{};
    // Get value
    ...
    return value.get<T>();
}

GCC 抱怨:

../utils.hpp:52:23: error: expected primary-expression before '>' token return value.get(); ^ ../utils.hpp:52:25: error: expected primary-expression before ')' token return value.get();

模板函数有什么问题?

我想你想要:

return value.template get<T>();

this 回答 good/comprehensive 描述了为什么...