WDC
WDC

Reputation: 344

Accessing common struct members in std::variant

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:

#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?

Upvotes: 3

Views: 1366

Answers (1)

Etienne Laurin
Etienne Laurin

Reputation: 7184

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);
}

Upvotes: 10

Related Questions