Last active
March 7, 2019 11:33
-
-
Save rinfz/0e10185c673c95a40cab973b4eeaec94 to your computer and use it in GitHub Desktop.
constexpr and std::function crap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <functional> | |
enum Dir { H, V }; | |
template<Dir d> | |
void call(double x, double y) { | |
std::function<double(double, double)> f; | |
if constexpr (d == H) { | |
f = [](double x, double y) -> double { | |
return x * y; | |
}; | |
} else { | |
f = [](double x, double y) -> double { | |
return x * y * 10; | |
}; | |
} | |
std::cout << f(x, y) << '\n'; | |
} | |
void call1() { | |
int n = 10; | |
std::function<bool()> f; | |
f = [&n]() -> bool { | |
return n > 15; | |
}; | |
std::cout << f() << '\n'; | |
n += 6; | |
std::cout << f() << '\n'; | |
} | |
int main() { | |
call<V>(5, 5); | |
call1(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment