Questions tagged [std-variant]

The class template std::variant represents a type-safe union. An instance of std::variant at any given time either holds a value of one of its alternative types, or in the case of error - no value

139 questions
9
votes
2 answers

What is the best way to disable implicit conversion from pointer types to bool when constructing an std::variant?

Consider the following: struct foo { }; struct bar { }; int main() { foo f; bar b; std::variant v; v = &b; // compiles in Visual Studio 19 v16.7.3 } As discussed in comments, I believe the above is legal C++17. There…
jwezorek
  • 8,592
  • 1
  • 29
  • 46
9
votes
1 answer

Is there boost::visit like std::visit, for boost::variant?

With C++14, I'm using boost::variant as a way of compile-time polymorphism: using MyType = boost::variant; Both classes have a method sayHello(). I'd like to call: MyType obj = ...; // either A() or B() boost::visit([](auto&& o) {…
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
8
votes
1 answer

Compare std::variant with int using C++20 <=> is not a constant expression

Since the std::variant is not allowed to compare with one of its alternative types in the standard library, I am implementing the compare function using C++20 <=> operator: template constexpr auto operator<=>(const…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
8
votes
1 answer

In C++, can I move something out of a std::variant?

I have a resource-wrapper class which is noncopyable, but is movable. Something like this (pseudeocode) class Wrapper { SomeResource* m_handle = nullptr; public: Wrapper(const Wrapper&) = delete; Wrapper& operator=(const Wrapper&) = delete; …
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
7
votes
1 answer

Can I make lambdas deduce the variant return type?

This mostly theoretical since I can always spell out the return type, but I wonder if there is a way for me to tell lambda that return type should be union(std::variant) of all returns in lambda body. #include #include struct…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
7
votes
1 answer

Why does clang handle exceptions for this trivial std::variant code?

If we have code like this: #include int main(){ using V = std::variant; V a = 5; V b = 5.6; a.swap(b); } https://gcc.godbolt.org/z/oqGiHs If you compile with clang, it emits code that handle exception during…
Nick
  • 9,962
  • 4
  • 42
  • 80
6
votes
1 answer

compiler error with std::variant - use of deleted function error

I have a class C that contains a struct S and a std::variant as members. The struct S has an int member a that is initialized to 0. Here is the code: #include class C { struct S { int a = 0; }; std::variant
Sir2B
  • 1,029
  • 1
  • 10
  • 17
6
votes
1 answer

well-known overloads for std::visit does not work with reference_wrapper

Here's a sample code: http://coliru.stacked-crooked.com/a/5f630d2d65cd983e #include #include template struct overloads : Ts... { using Ts::operator()...; }; template overloads(Ts &&...) ->…
slyx
  • 2,063
  • 1
  • 19
  • 28
6
votes
1 answer

std::variant of std::string inside any class in constexpr context fails to compile

The following code #include #include #include constexpr bool USE_VARIANT = 1; using T = std::conditional_t, std::string>; struct S { T t; }; constexpr int func() { S…
LHLaurini
  • 1,737
  • 17
  • 31
6
votes
3 answers

How can classes with `std::variant` members be copied safely?

The following sample builds and runs properly with the line Container container2(container1); removed. It appears the copy constructor for std::variant itself is deleted, which makes my Container's copy constructor implicitly deleted. Effectively,…
6
votes
2 answers

get currently held typeid of std::variant (like boost::variant type())

I've migrated from boost::variant to std::variant, and hit a snag. I was using a nice function in boost 'type()' that lets you get the currently held typeid. See https://www.boost.org/doc/libs/1_48_0/doc/html/boost/variant.html#id1752388-bb How can…
GreekFire
  • 359
  • 4
  • 15
5
votes
2 answers

How do I tell if a std::variant holds any value at all?

I already know how to use std::variant fairly well with std::get_if(), std::get() and std::visit(). But, what if I just want a simple way to tell if a variant has ever been initialized to any value? That is, I don't care what the value is, I just…
Joe
  • 5,394
  • 3
  • 23
  • 54
5
votes
1 answer

Assignment operator of std::variant of custom type with deleted special member functions?

Consider: #include struct A { A() = default; A(A&&) = delete; }; struct B { B() = delete; B(A&&) {}; }; int main() { std::variant v{}; v = A{}; } MSVC accepted it, while GCC and Clang rejected it with the same error…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
5
votes
2 answers

is there a way to use std::variant with arbitrarily many defined types?

I am just learning to use std::variant and I would like to declare a type list consisting of (in principle) arbitrarily many of my user defined types. I.e., something like template struct MyType{ T x; }; template
user366202
  • 249
  • 2
  • 7
5
votes
1 answer

StateMachine with std::variant, getting the custom template deduction right

With the following code how do I write the custom template deduction correctly? template struct visitor : Ts... { using Ts::operator()...; }; template visitor(State,…
schultz
  • 316
  • 2
  • 14
1
2
3
9 10