std::variant and std::visit: error: no member named 'valueless_by_exception'

std::variant and std::visit: error: no member named 'valueless_by_exception'

我有以下代码块:

std::visit([&](auto &&arg)
{
    using T = std::decay_t<decltype(arg)>;

    if constexpr (
        std::is_same_v<T, size_t> ||
        std::is_same_v<T, int> ||
        std::is_same_v<T, double> ||
        std::is_same_v<T, bool>
    ) {
        document->AddMember(value.first.c_str(), arg, allocator);
    }
    else if constexpr (std::is_same_v<T, std::string>){
        document->AddMember(
            rapidjson::StringRef(value.first.c_str()),
            rapidjson::StringRef(arg.c_str()),
            allocator
        );
    }
    else if constexpr (std::is_same_v<T, std::shared_ptr<Serializable>>) {
        // TODO
    }
    else if constexpr (std::is_same_v<T, std::vector<std::string>>) {
        for (const auto &strVal: arg) {
            // TODO
        }
    }
    else if constexpr (std::is_same_v<T, std::vector<std::shared_ptr<Serializable>>>) {
        for (const auto &strVal: arg) {
            // TODO
        }
    }
    else {
        throw UndefinedException("Invalid type encountered in instance of serial::Serializable");
    }
}, value);

当我尝试编译它时,出现以下错误:

/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/variant:1651:23: error: no member named 'valueless_by_exception' in 'std::pair<const std::__cxx11::basic_string<char>, std::variant<unsigned long, int, double, bool, std::__cxx11::basic_string<char>, std::shared_ptr<trogdor::serial::Serializable>, std::vector<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char> > >, std::vector<std::shared_ptr<trogdor::serial::Serializable>, std::allocator<std::shared_ptr<trogdor::serial::Serializable> > > > >'
      if ((__variants.valueless_by_exception() || ...))

我无法弄清楚这意味着什么或如何解决它。任何人都可以帮助我了解发生了什么以及如何修改我的代码以使其编译?

std::visit (value) 的参数应该是 std::variant 但似乎是 std::pair.

示例:

#include <string>
#include <utility>
#include <variant>

int main () {
    std::variant<std::string> foo;

    std::visit([](auto&&){}, foo);   // OK

    std::pair<std::string, std::variant<std::string>> bar;

    std::visit([](auto&&){}, bar);   // same error as you have
}

可能的解决方案:

std::visit([&](auto&& arg) {
    // ....
}, value.second);