std-variant Questions
3
Let's say I am writing a class that passes a std::variant<A,B> to another class.
While I design the class, I decide I'd like to keep a copy of the last thing that was passed to that other cla...
Audwen asked 5/11 at 14:31
4
Solved
For example, it should be very helpful to equal compare a std::variant<T1, T2> with a T1 or T2. So far we can only compare with the same variant type.
Pricilla asked 20/3, 2019 at 18:15
2
Solved
Please explain what is the difference between union and std::variant and why std::variant was introduced into the standard? In what situations should we use std::variant over the old-school union?
...
Flipflop asked 7/2, 2017 at 5:23
2
For example, I'm trying to implement an AST using std::variant, where a Token can be a number, keyword, or variable. Where numbers are represented by the int and keywords and variables are represen...
Extemporize asked 7/9, 2023 at 7:45
2
Solved
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...
Ape asked 28/7, 2023 at 22:40
1
Solved
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 <variant>
class C
{
struct S {...
Muscadel asked 18/7, 2023 at 16:6
3
Solved
I would like to filter repeated types from a std::variant like this:
std::variant<int, double, int, std::string, int> -> std::variant<int, double, std::string>
std::variant<std::s...
Tica asked 30/5, 2020 at 20:10
2
Solved
I am trying to learn std::variant. I do not understand why in this example, where I prefer not to initialize ab yet, and I use std::monostate for that, the class A gets constructed once, but destru...
Madelenemadelin asked 15/3, 2023 at 7:11
3
Solved
I've never used std::get_if, and since its name is different from std::get, I don't see a reason why its argument should be a pointer¹ (whereas std::get has a by-reference parameter).
¹If it was n...
Tracee asked 21/9, 2021 at 14:41
1
Solved
Here's a sample code: http://coliru.stacked-crooked.com/a/5f630d2d65cd983e
#include <variant>
#include <functional>
template<class... Ts> struct overloads : Ts... { using Ts::ope...
Frans asked 11/12, 2022 at 8:51
4
Solved
I am using C++17's std::visit() function on a variant with many alternatives, and the error messages produced by the compiler whenever I forget one or more of the alternatives in my visitor are qui...
Heyes asked 5/6, 2022 at 13:9
1
Solved
I have some std::variant classes, each with several alternatives, and I would like to define a visitor class template that takes a variant as its template parameter and will automatically define a ...
Accessary asked 5/6, 2022 at 18:52
4
I'm wondering about std::variant performance. When should I not use it? It seems like virtual functions are still much better than using std::visit which surprised me!
In "A Tour of C++" ...
Pinsky asked 30/8, 2019 at 12:3
4
Solved
I have some var = std::variant<std::monostate, a, b, c> when a, b, c is some types.
How, at runtime, do I check what type var contains?
In the official documentation I found information that ...
Circumrotate asked 19/8, 2020 at 7:32
1
Solved
The following code
#include <optional>
#include <string>
#include <variant>
constexpr bool USE_VARIANT = 1;
using T = std::conditional_t<USE_VARIANT, std::variant<std::str...
Oscine asked 31/12, 2021 at 11:5
1
Solved
This is mostly trivia question, as I doubt that I will ever need this space saving.
While playing around on godbolt I noticed that both libstdc++ and libc++ implementations of std::variant require ...
Coreycorf asked 3/9, 2021 at 19:24
1
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 bo...
Thayer asked 9/2, 2021 at 13:32
1
Solved
Update: This is a C++ standard defect, which is fixed in C++20 (P0608R3). Also, VS 2019 16.10 has fixed this bug with /std:c++20.
MSVC 19.28 rejects the following code but gcc 10.2 accepts it and o...
Condescension asked 1/1, 2021 at 14:19
2
Solved
Consider the following:
struct foo {
};
struct bar {
};
int main()
{
foo f;
bar b;
std::variant<foo*, bool> v;
v = &b; // compiles in Visual Studio 19 v16.7.3
}
As discussed in com...
Sarasvati asked 7/10, 2020 at 18:3
3
Solved
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...
Boxcar asked 10/10, 2020 at 1:46
1
Solved
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 <typen...
Wallaroo asked 6/8, 2020 at 5:56
1
Solved
If we have code like this:
#include <variant>
int main(){
using V = std::variant<int, double>;
V a = 5;
V b = 5.6;
a.swap(b);
}
https://gcc.godbolt.org/z/oqGiHs
If you compile wi...
Fritzsche asked 16/6, 2020 at 8:29
3
Solved
How to by a given variant type
using V = std::variant<bool, char, std::string, int, float, double, std::vector<int>>;
declare two variant types
using V1 = std::variant<bool, char...
Jasun asked 4/3, 2020 at 14:38
1
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&) = de...
Facetiae asked 14/2, 2020 at 1:21
4
Solved
I'm playing around with callback functions and wish to register multiple functions via std::bind that differ in signatures (altough they all return void). Assigning the result of the std::bind to t...
Furfuran asked 29/8, 2019 at 11:41
1 Next >
© 2022 - 2024 — McMap. All rights reserved.