Tutorial by Examples

This creates a variant (a tagged union) that can store either an int or a string. std::variant< int, std::string > var; We can store one of either type in it: var = "hello"s; And we can access the contents via std::visit: // Prints "hello\n": visit( [](auto&&a...
This is an advanced example. You can use variant for light weight type erasure. template<class F> struct pseudo_method { F f; // enable C++17 class type deduction: pseudo_method( F&& fin ):f(std::move(fin)) {} // Koenig lookup operator->*, as this is a pseudo-method...
This does not cover allocators. struct A {}; struct B { B()=default; B(B const&)=default; B(int){}; }; struct C { C()=delete; C(int) {}; C(C const&)=default; }; struct D { D( std::initializer_list<int> ) {}; D(D const&)=default; D()=default; }; std::variant<A,B> var_ab...

Page 1 of 1