Boost Variant是否提供与std的hold_alternative类似的功能?

时间:2018-08-09 10:00:13

标签: c++ boost

我在cppreference.com上找到了此代码。我想知道boost是否为其变体类型提供类似的功能。我发现boost文档真的很糟糕,什么也找不到。

int main()
{
    std::variant<int, std::string> v = "abc";
    std::cout << std::boolalpha
              << "variant holds int? "
              << std::holds_alternative<int>(v) << '\n'
              << "variant holds string? "
              << std::holds_alternative<std::string>(v) << '\n';
}

3 个答案:

答案 0 :(得分:4)

尽管不完全相同,但是您可以使用基于指针的Dropzone.options.dropzoneForm = { url : "http://192.168.1.69:8090/Cubix/upload.web", autoProcessQueue : true, uploadMultiple : true, maxFilesize : 256, // MB parallelUploads : 1, crossDomain: true, withCredentials: false, maxFiles : 100, addRemoveLinks : true, previewsContainer : ".dropzone-previews" 函数:

get

答案 1 :(得分:3)

否,但是,您可以使用create table test as with ord as ( select rownum order_id, date'2018-01-01' + rownum / 24 datetimecreated from dual connect by level <= 100000), act as ( select 'A1' action_id, 'O1' object_id, 1 offset from dual union all select 'A1' action_id, 'O2' object_id, 2 offset from dual union all select 'A1' action_id, 'O3' object_id, 3 offset from dual union all select 'A1' action_id, 'O4' object_id, 4 offset from dual union all select 'A2' action_id, 'O1' object_id, 5 offset from dual union all select 'A2' action_id, 'O2' object_id, 6 offset from dual union all select 'A2' action_id, 'O3' object_id, 7 offset from dual union all select 'A2' action_id, 'O4' object_id, 8 offset from dual union all select 'A3' action_id, 'O1' object_id, 9 offset from dual union all select 'A3' action_id, 'O2' object_id, 10 offset from dual union all select 'A3' action_id, 'O3' object_id, 11 offset from dual union all select 'A3' action_id, 'O4' object_id, 12 offset from dual union all select 'A4' action_id, 'O1' object_id, 13 offset from dual union all select 'A4' action_id, 'O2' object_id, 14 offset from dual union all select 'A4' action_id, 'O3' object_id, 15 offset from dual union all select 'A4' action_id, 'O4' object_id, 16 offset from dual union all select 'A5' action_id, 'O1' object_id, 17 offset from dual union all select 'A5' action_id, 'O2' object_id, 18 offset from dual union all select 'A5' action_id, 'O3' object_id, 19 offset from dual union all select 'A5' action_id, 'O4' object_id, 20 offset from dual) select order_id, datetimecreated + offset datetimecreated, action_id,object_id from ord cross join act; create index test_idx1 on test(action_id, object_id,datetimecreated); create index test_idx2 on test(order_id,action_id, object_id); 方法:

type()

但是它不能保护您避免两次拥有与std :: holds_alternative相同的类型(#include <iostream> #include <boost/variant.hpp> int main() { boost::variant<int, std::string> v = "abc"; std::cout << std::boolalpha << "variant holds int? " << (v.type() == typeid(int)) << '\n' << "variant holds string? " << (v.type() == typeid(std::string)) << '\n'; } )。

答案 2 :(得分:0)

您可以创建一个简单的包装程序,该包装程序将像标准包装程序一样工作。使用boost::get具有多个重载的事实,并且在传递指针时,它还将返回一个(可能为null)指针。

template <typename T, typename... Ts>
bool holds_alternative(const boost::variant<Ts...>& v) noexcept
{
    return boost::get<T>(&v) != nullptr;
}

它也会被ADL拾取,所以放在哪里都没关系。