【问题标题】:How to retrieve mapbox::util::variant value in a templated function如何在模板化函数中检索 mapbox::util::variant 值
【发布时间】:2017-01-23 13:24:14
【问题描述】:

使用 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: 错误: '>' 之前的预期主表达式 令牌 返回值.get(); ^ ../utils.hpp:52:25: 错误: ')' 标记之前的预期主表达式 返回值.get();

模板函数有什么问题?

【问题讨论】:

  • 做 value.template get();帮忙?
  • 糟糕,是的,它有帮助。请将其发布为答案,以便我将其标记为正确。

标签: c++ templates mapbox variant


【解决方案1】:

我想你想要:

return value.template get<T>();

this 的回答给出了一个很好/全面的描述为什么......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多