3

I have trouble understanding how to use std::variant in C++17. Given two struct A and B, and a std::vector<std::variant<A,B>> vs, I would like to:

  • Refer to a common struct member, e.g. n;
  • Call a common function, e.g. fun() or add().
#include <iostream>
#include <variant>
#include <vector>

struct A {
    int n;
    void fun() { std::cout << "fun\n"; }
    int add(int m) { return n+m; }
};
struct B {
    int n;
    void fun() { std::cout << "fun\n"; }
    int add(int m) { return n+m; }
};

int main() {
    std::vector<std::variant<A,B>> vs;
    vs.push_back(A{10,11});
    vs.push_back(B{20,22});
    
    // How to refer to struct members without using std::get<v.index()>(v)?
    for (auto && v : vs) {
         // 1. How to refer to v.n?
         // 2. How to call v.fun()?
         // 3. How to call v.add() with input parameter m?
    }
}

I am told to use std::visit, but am too blunt to understand how it works. Could anyone show a simple example here?

Etienne Laurin
  • 6,731
  • 2
  • 27
  • 31
WDC
  • 334
  • 1
  • 3
  • 14

1 Answers1

9

Use std::visit with a lambda that has an auto&& parameter to access members that are common to all variant types. In your example:

for (auto&& v : vs) {
    std::visit([&](auto&& x){
        std::cout << x.n << x.add(1);
        x.fun();
    }, v);
}
Etienne Laurin
  • 6,731
  • 2
  • 27
  • 31
  • Why `&&` if we don't intend to move from it? – Vorac Aug 02 '21 at 22:18
  • 1
    @Vorac See https://stackoverflow.com/questions/13230480/what-does-auto-tell-us and https://stackoverflow.com/questions/13241108/why-does-a-range-based-for-statement-take-the-range-by-auto – Etienne Laurin Aug 03 '21 at 17:00