Skip to content

Instantly share code, notes, and snippets.

@daixtrose
Last active April 4, 2018 14:59
Show Gist options
  • Save daixtrose/5e8b6c7b0dac7d5b76e692ab2283ad10 to your computer and use it in GitHub Desktop.
Save daixtrose/5e8b6c7b0dac7d5b76e692ab2283ad10 to your computer and use it in GitHub Desktop.
C++ overload resolution with local lambda and global functions
#include <typeinfo>
#include <iostream>
#include <string>
namespace {
struct Something {
};
template <typename T>
void process(const T& x, Something const& s)
{
std::cerr << "Processing " << typeid(T).name() << std::endl;
}
struct A {
};
void process(A const& p, Something const& s)
{
std::cout << "A" << std::endl;
}
struct B {
};
void process(B const& p, Something const& s)
{
std::cout << "B" << std::endl;
}
} // namespace
template<class... T> struct overloaded : T... { using T::operator()...; };
template<class... T> overloaded(T...) -> overloaded<T...>;
int main()
{
struct C {};
auto process = overloaded{
[](C const& p, Something const& s) {
std::cerr << "Processing C" << std::endl;
},
[](const auto& p, Something const& s){::process(p, s);}
};
Something s{};
A a{};
B b{};
C c{};
process(a, s);
process(b, s);
process(c, s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment