Does Boost Variant provides a similar function as std's holds_alternative?

std::variant
std::variant switch

I found this code on cppreference.com. I was wondering if boost provides a similar function for its variant type. I found the boost documentation really awful and can't find anything.

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';
}

No but, you can use the type() method:

#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';
}

But it will not protect you against having the same type twice (boost::variant<int, int, std::string>) as std::holds_alternative would do.

Boost.Variant2: A never valueless variant type, Whereas standard containers such as std::vector may be thought of as "multi-​value, single Notable features of boost::variant include: (Plain Old Data) types, and so does not accept types exhibiting non-trivial construction or destruction: Whereas standard containers such as std::vector may be thought of as "multi-value, single type," variant is "multi-type, single value." Notable features of boost::variant include: Full value semantics, including adherence to standard overload resolution rules for conversion operations.


Although not exactly the same, you can use the pointer based get function:

boost::variant<int, std::string> v = "abc";

std::cout << std::boolalpha
          << "variant holds int? "
          << (boost::get<int>(&v) != nullptr) << '\n'
          << "variant holds string? "
          << (boost::get<std::string>(&v) != nullptr) << '\n';

Chapter 45. Boost.Variant - 1.64.0, Boost.Variant provides a class called boost::variant that resembles union . In Example 24.1, v can store values of type double , char , or std::string . If overloaded operators are equivalent in functionality, the code can be simplified by using  The only requirement is that the types must have been passed as template parameters to boost::variant so they are known to the boost::variant variable. boost::variant supports any type. For example, it is possible to store a std::string in a boost::variant variable – something that wasn’t possible with union before C++11.


You can create a simple wrapper that will work just like the standard one. Use the fact that boost::get has multiple overloads and when passed a pointer, it will also return a (possibly null) pointer.

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

It will be also picked up by ADL, so it doesn't matter much where you put it.

Chapter 24. Boost.Variant, So, I started a new project, and I do use boost::variant to be able to struct NameVisitor : boost::static_visitor<std::string> { template<class T> gives a very clear interface for the template method: it has a single So, at least currently, C++ has no (to me) known mechanism to have a method pointer like  The variant class calls destructors and constructors of non-trivial types, so in the example, the string object is cleaned up before we switch to new variants. I’d say that unless you’re doing some low-level stuff, possibly only with simple types, then unions might still be ok. But for all other uses cases, where you need variant types, std


boost::variant and a general, generic visitor class, Library feature-test macros · Date and time Integer comparison functions The helper class std::monostate can be used to make such variants calls the provided functor with the arguments held by one or more variants For instance, while it is immediately clear how one might write a function accepting a specific variant instantiation, say variant<int, std::string>, it is less clear how one might write a function accepting any given variant.


std::variant, Bison provides a variant based implementation of semantic values for C++. in the previous section, and in particular, object types can be used without pointers. %union { int ival; std::string* sval; } %token <ival> NUMBER; %token <sval> STRING; Since C++ features destructors, and since it is customary to specialize​  typedef boost::variant< int , boost::recursive_wrapper< binary_op<add> > , boost::recursive_wrapper< binary_op<sub> > > expression; Because variant provides special support for recursive_wrapper, clients may treat the resultant variant as though the wrapper were not present.


C++ Variants (Bison 3.5), This software is provided "as is" without express or implied warranty, and with no Usually though, we would like to do more with the content of a variant than instance, if we were to write a function accepting a variant<int, std::string>, we  Botet C++ generic match function P0050 template < class Ts > struct type_list {}; Using the match function to inspect one sum type Given the boost::variant sum type, we could just visit it using the proposed overload