如何正确地使用std::variant替换boost::variant?

7

使用 boost:variant

#include <tuple>
#include <iostream>
#include <boost/variant.hpp>

template <size_t n, typename... T>
boost::variant<T...> _tuple_index(size_t i, const std::tuple<T...>& tpl) {
    if (i == n)
        return std::get<n>(tpl);
    else if (n == sizeof...(T) - 1)
        throw std::out_of_range("Out of Index");
    else
        return _tuple_index<(n < sizeof...(T)-1 ? n+1 : 0)>(i, tpl);
}
template <typename... T>
boost::variant<T...> tuple_index(size_t i, const std::tuple<T...>& tpl) {
    return _tuple_index<0>(i, tpl);
}

template <typename T>
auto tuple_len(T &tpl) {
    return std::tuple_size<T>::value;
}

int main()
{
    std::tuple<std::string, double, double, int> t("123", 4.5, 6.7, 8);
    for(int i = 0; i != tuple_len(t); ++i) {
        std::cout << tuple_index(i, t) << std::endl; // works with boost
    }
}

boost::variant替换为std::variant,添加了一个助手来对std::variant进行流式处理:

#include <tuple>
#include <iostream>
#include <variant>

template <size_t n, typename... T>
std::variant<T...> _tuple_index(size_t i, const std::tuple<T...>& tpl) {
    if (i == n)
        return std::get<n>(tpl);
    else if (n == sizeof...(T) - 1)
        throw std::out_of_range("Out of Index");
    else
        return _tuple_index<(n < sizeof...(T)-1 ? n+1 : 0)>(i, tpl);
}
template <typename... T>
std::variant<T...> tuple_index(size_t i, const std::tuple<T...>& tpl) {
    return _tuple_index<0>(i, tpl);
}

template <typename T>
auto tuple_len(T &tpl) {
    return std::tuple_size<T>::value;
}

// added helper to stream std::variant
template <typename T0, typename ... Ts>
std::ostream & operator<< (std::ostream & s, std::variant<T0, Ts...> const & v) { 
    std::visit([&](auto && arg){ s << arg;}, v); 
    return s;
}

int main()
{
    std::tuple<std::string, double, double, int> t("123", 4.5, 6.7, 8);
    for(int i = 0; i != tuple_len(t); ++i) {
        std::cout << tuple_index(i, t) << std::endl; // doesn't work anymore
    }
}

编译仍然存在错误:
$ clang++ -v                                                                                                                                                                                                                                                            [17:37:47]
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

$ clang++ -std=c++17 isostd.cpp

isostd.cpp:8:16: error: no viable conversion from returned value of type 'const typename tuple_element<1UL, tuple<basic_string<char>, double, double, int> >::type' (aka 'const __type_pack_element<1UL, std::__1::basic_string<char>, double, double, int>') to function return
      type 'std::variant<basic_string<char>, double, double, int>'
        return std::get<n>(tpl);
               ^~~~~~~~~~~~~~~~
isostd.cpp:12:16: note: in instantiation of function template specialization '_tuple_index<1, std::__1::basic_string<char>, double, double, int>' requested here
        return _tuple_index<(n < sizeof...(T)-1 ? n+1 : 0)>(i, tpl);
               ^
isostd.cpp:16:12: note: in instantiation of function template specialization '_tuple_index<0, std::__1::basic_string<char>, double, double, int>' requested here
    return _tuple_index<0>(i, tpl);
           ^
isostd.cpp:35:22: note: in instantiation of function template specialization 'tuple_index<std::__1::basic_string<char>, double, double, int>' requested here
        std::cout << tuple_index(i, t) << std::endl; // doesn't work anymore
                     ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/variant:1142:3: note: candidate constructor not viable: no known conversion from 'const typename tuple_element<1UL, tuple<basic_string<char>, double, double, int> >::type'
      (aka 'const __type_pack_element<1UL, std::__1::basic_string<char>, double, double, int>') to 'const std::__1::variant<std::__1::basic_string<char>, double, double, int> &' for 1st argument
  variant(const variant&) = default;
  ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/variant:1143:3: note: candidate constructor not viable: no known conversion from 'const typename tuple_element<1UL, tuple<basic_string<char>, double, double, int> >::type'
      (aka 'const __type_pack_element<1UL, std::__1::basic_string<char>, double, double, int>') to 'std::__1::variant<std::__1::basic_string<char>, double, double, int> &&' for 1st argument
  variant(variant&&) = default;
  ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/variant:1155:13: note: candidate template ignored: substitution failure [with _Arg = const double &, $1 = 0, $2 = 0, $3 = 0, _Tp = double]: no member named 'value' in
      'std::__1::__find_detail::__find_unambiguous_index_sfinae<double, std::__1::basic_string<char>, double, double, int>'
  constexpr variant(_Arg&& __arg) noexcept(
            ^
1 error generated.

如何正确地用std::variant替换boost:variant?

我已经看过这篇文章:What are the differences between std::variant and boost::variant?

Boost.Variant包括recursive_variant,它允许一个variant包含自己。它们本质上是指向boost::variant的指针的特殊包装器,但它们与visit机制相关联。

如果我理解正确,没有办法完成替换吗?


1
更简单的复现方式:https://godbolt.org/z/X37pRo https://godbolt.org/z/UOsGFT - Justin
1
这里我回答了类似的问题 https://stackoverflow.com/a/56886345/1387438 并提供了使用 std::variant 解决完全相同问题的 工作示例 - Marek R
1个回答

6

由于您的变量中存在重复类型,因此部分构造函数已被禁用:

如果Types中恰好有一个T的出现,则此重载仅参与重载决策...

您需要使用具有显式类型索引的构造函数:

return std::variant<T...>(std::in_place_index<n>, std::get<n>(tpl));

您原来的boost代码存在未定义行为

每个作为模板参数指定给variant的类型,必须在去除限定符后是不同的。因此,例如,variant<int, int>variant<int, const int>都具有未定义行为。

标准库实现支持重复类型,因此可以防止意外构造一个模棱两可的variant。例如,下面的代码应该怎么做:

variant<std::string, double, double, int> t = 4.5;

使用boost库会导致未定义行为,其中任何一个双精度值都可能被初始化或执行完全不同的操作。标准库明确将此作为编译器错误,因此您必须选择要初始化的哪个双精度值。

1
概念验证(Proof of Concept)(左侧为 Boost,右侧为 STL)。 - Xirema

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接