Skip to content

C++ overload header for visitor pattern. 🇰🇷 방문자 패턴에 대한 C++ 오버로드 헤더

License

Notifications You must be signed in to change notification settings

JayTwoLab/overload.cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

overload.cpp

English , Korean

  • overload.cpp is a project that helps make it easier to implement the visitor pattern in C++17 .
  • When using std::visit, you can easily overload multiple lambda functions (anonymous functions) to create concise processing for different types.

How to use

  • Include the overload.hpp header file in the project.
  • When using std::visit, use the 'overload' structure to overload multiple lambda functions.

Sample code

  • Code
#include <iostream>
#include <variant>
#include <string>

#include "overload.hpp" // [0] Just Include overload header.

int main() {
    std::variant<int, std::string> v; // v is integer type or string type.

    v = 42; // [1] Set integer type
    std::visit(overload{
                   [](int i) { std::cout << "[1] Integer type: " << i << '\n'; },
                   [](const std::string& s) { std::cout << "[1] String type: " << s << '\n'; }
               }, v);

    v = "Hello";  // [2] Set string type
    std::visit(overload{
                   [](int i) { std::cout << "[2] Integer type: " << i << '\n'; },
                   [](const std::string& s) { std::cout << "[2] String type: " << s << '\n'; }
               }, v);

    return 0;
}
  • Output
[1] Integer type: 42
[2] String type: Hello

License

  • MIT License

Contact

About

C++ overload header for visitor pattern. 🇰🇷 방문자 패턴에 대한 C++ 오버로드 헤더

Resources

License

Stars

Watchers

Forks

Packages

No packages published