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
0
votes
1 answer

C++ compiler error: use of deleted function std::variant()

I am continually getting the following error message telling me that I am using a deleted function, which I think is the std::variant default constructor. In file included from main.cpp:2: Document.hpp: In instantiation of ‘Document
0
votes
1 answer

Check if variant has value at compile time

Can't use variant.index() in constexpr statements so need to iterate variant and return true if it can cast to some type or false if it's emtpy or contains zero value. Try this code, but seems index sequence is not variadic type and ... operator not…
0
votes
1 answer

Variant with variadic templates

Sorry if this case is rather complicated, but I hope it helps people to better understanding modern c++ usage. So I want to make this code works. It should produce special lambdas for single integral type and for variant type for calculate sequence…
0
votes
1 answer

How to use TEMPLATE_TEST_CASE with pairs of types?

I am trying to use catch2 TEMPLATE_TEST_CASE for pairs of types, i.e. instead of templating a single type for each test, I need to use a correlated pair of types. I thought I could use std::variant to store these pairs, but compilation fails with:…
Armut
  • 969
  • 8
  • 22
0
votes
2 answers

define a function to return specified type of std::variant c++

I am almost new to using c++17 rules. My question is straightforward; how can I access the std::variant types in the same order I defined them? Something like the following code that I know is not working! #include #include…
Behzad
  • 121
  • 1
  • 16
0
votes
2 answers

Overriding std::variant::operator==

I'm a little surprised that putting the variant's alternative types into a sub-namespace seems to break the operator==: #include #include namespace NS { namespace structs { struct A {}; struct B {}; } // namespace…
Rai
  • 1,328
  • 11
  • 20
0
votes
1 answer

Unordered map vs map

Does anyone know why Dict class is invalid but Dict2 is fine? #include #include #include #include class Dict { public: Dict() {} private: std::unordered_map
Keivan
  • 673
  • 7
  • 15
0
votes
2 answers

Why std::unique_ptr isn't optimized while std::variant can?

I tried to compare the overhead of std::visit(std::variant polymorphism) and virtual function(std::unique_ptr polymorphism).(please note my question is not about overhead or performance, but optimization.) Here is my…
Dickless
  • 23
  • 4
0
votes
1 answer

Call BaseState function instead of Derived function when using std::variant<...>

I am experimenting with finate state machines and std::variant<..> std::variant will hold all possible states The states will be defined in State classes. What I wanted was to have a BaseState class, in which "generic events" should be handled. With…
hreintke
  • 51
  • 4
0
votes
2 answers

Assignment operator for std::variant

I'm trying to assign a float to a defined value in my std::map using std::variant. I initialized my map this way: std::map kwargs; kwargs["type"] = "linear"; kwargs["flag"] = true;…
user13549804
0
votes
1 answer

Provide a operator== for std::variant

I am trying to create an operator== operator for an std::variant defined in the map like this: struct anyType { template void operator()(T t) const { std::cout << t; } void operator()(const std::string& s) const { std::cout <<…
user13549804
0
votes
1 answer

Throw exception on missing function overload with std::variant instead of compile time error

This is a follow up to this question Consider the following code #include int add_(int a, int b){ return a+b; } float add_(float a, float b){ return a+b; } float add_(int a, float b){ return a+b; } float add_(float a, int…
infinitezero
  • 1,610
  • 3
  • 14
  • 29
0
votes
1 answer

Does std::variant provide functionality similar to boost::variant<>::types?

boost::variant exposes its list of variant types via boost::variant<>::types, which can be conveniently used with boost::mpl::for_each. std::variant is missing such a member. I see std::variant_alternative is provided. Can this be used to produce a…
Braden
  • 1,448
  • 10
  • 11
0
votes
1 answer

How can I pass the current alternative type of std::variant to a callable?

I'm invoking some user-defined callables with arguments determined at runtime. I have the following working: using argument_t = std::variant< int, bool, double, std::string >; using argument_list = std::vector< argument_t >; template < typename…
yano
  • 4,095
  • 3
  • 35
  • 68
0
votes
1 answer

How to compare std::variant of custom classes?

1.It's my classes: struct SymbolToken { std::string name; }; struct QuoteToken { }; struct ConstantToken { int value; }; 2. I have a following typedef: typedef std::variant Token; 3. I wanna write…
1 2 3
9
10