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
4
votes
3 answers

Cleaner way to specify type to get from a std::variant?

I've got code that can be simplified to std::variant v[2] = foo(); int a = std::get(v[0]); float b = std::get(v[1]); Obviously this can go throw if foo() returns the wrong variants, but that's not my problem…
MSalters
  • 173,980
  • 10
  • 155
  • 350
4
votes
1 answer

How to wrap C++17 std::variant with SWIG for use in python?

I am trying to wrap in Python with SWIG some C++17 code that uses std::variant. I've found this answer (https://stackoverflow.com/a/58513139) about wrapping boost::variant and I managed to adapt the code so that it works with std::variant instead.…
A.Manfreda
  • 63
  • 5
4
votes
1 answer

g++ std::variant seems can't support user class with std::atomic/std::mutex variable member (with detail/code)

If I have a class with std::atomic_bool or std::mutex member for example, and if I put this class inside std::variant, my g++ will complain with "no matching function for call to std::variant<....>". Now I have to declare my std::mutex member to be…
MingC
  • 83
  • 4
4
votes
2 answers

How to use alternative types of std::variant polymorphically

I am writing a parser and am using a std::variant to represent the nodes of the AST. For basic expressions, I have something like: struct Add; struct Sub; struct Mul; struct Div; typedef std::variant Node; (The actual structure…
Willis Blackburn
  • 8,068
  • 19
  • 36
4
votes
5 answers

Predefined type list passed to a std::variant

Is there any way to create a pre-defined type list, and use those types in a std::variant in c++ 17? Here is what I'm trying to do, it compiles, but doesn't work as I was hoping: template < class ... Types > struct type_list {}; using valid_types =…
David Massat
  • 181
  • 1
  • 1
  • 8
3
votes
2 answers

How do you write a function template that determines if two arbitrary variants are holding the same type?

Consider the following function for determining if variables of identical variant types are holding the same type: #include #include template bool hold_same_types(const std::variant& v1, const…
jwezorek
  • 8,592
  • 1
  • 29
  • 46
3
votes
2 answers

Why does std::variant behave differently on GCC 8.5 and GCC 12.1 in respect to a `const char *` literal?

#include #include #include int main() { std::variant v{ "hasta la vista" }; std::cout << std::boolalpha << std::holds_alternative(v) << ' ' << std::holds_alternative(v) <<…
user7610
  • 25,267
  • 15
  • 124
  • 150
3
votes
1 answer

Techniques for cutting down on verbosity when do polymorphism via std::variant rather than inheritance

Say you have entities in a 2D game framework or something similar -- e.g. a GUI framework -- where there are various types of entities that share common properties like position and rotation but where some of these properties must be handled on a…
jwezorek
  • 8,592
  • 1
  • 29
  • 46
3
votes
2 answers

C++17 std::variant is slower than dynamic polymorphism?

I was following this blog and was trying to replace a dynamic polymorphism code into using std::variant and std::visit. But I can not get std::variant + std::visit to work better than a virtual struct impl. Its about 1.3-1.5x slower! (GCC 10.3 -O3…
n1r44
  • 581
  • 1
  • 4
  • 12
3
votes
1 answer

Observing the state of a variant during construction

Consider the following code: #include #include struct foo { foo() noexcept; foo(const foo&) noexcept = default; foo(foo&&) noexcept = default; foo& operator=(const foo&) noexcept = default; foo& operator=(foo&&)…
Jean-Michaël Celerier
  • 7,412
  • 3
  • 54
  • 75
3
votes
1 answer

inconsistent behavior in regard to passing a pointer to a C++17 Standard Variant to function expecting const reference

I'm seeing some odd behavior in regard to passing a pointer into a function that expects a constant reference. Obviously C++ expects me to de reference the pointer before I pass it into the function expecting the reference. I'm looking for an error…
Timothy John Laird
  • 1,101
  • 2
  • 13
  • 24
3
votes
2 answers

Why does assigning an int to a std::variant fail?

I feel like I'm missing something obvious about int type promotion when assigning to a variant. On gcc version 9.3.0 (Ubuntu 9.3.0-11ubuntu0~18.04.1), compiling with -std=c++17, the following code fails to compile: #include #include…
3
votes
1 answer

return value from possible type in std::variant through std::visit

I am trying to wrap my head around std::variant and std::visit and I am trying to come up with a method to specify a couple of types that I would like my variable to hold (which would go into my std::variant) and then retrieve stored data through a…
tom
  • 361
  • 3
  • 11
3
votes
2 answers

std::visit a std::variant with overloaded free-function instead of function-object

In C++17 is there a simple way to std::visit a variant with an overloaded free-function or must I use an object with an overloaded call operator? In other words, is it possible to add something simple to make the following //ERROR! line compile to…
Timtro
  • 418
  • 5
  • 15
3
votes
1 answer

Compile error when using std::variant with gmock 1.8 object

Let's have this code using gmock 1.8: #include "gtest/gtest.h" #include "gmock/gmock.h" #include class Obj { public: MOCK_METHOD0( mock, void() );//<-!!! }; using Variant = std::variant; TEST(Test, Test) { Obj obj; …
AshleyWilkes
  • 741
  • 5
  • 13
1 2
3
9 10