Created
December 6, 2016 01:54
-
-
Save benloong/edda39904496c49b1bbe0d8d135f435e to your computer and use it in GitHub Desktop.
c++: inheritance lambda ref https://www.youtube.com/watch?v=W-xTpqj31mI&feature=share
This file contains hidden or 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
template<typename L1, typename L2> | |
struct S : L1, L2 | |
{ | |
S(L1 l1, L2 l2) :L1( std::move(l1) ), L2( std::move(l2) ) | |
{ | |
} | |
using L1::operator(); | |
using L2::operator(); | |
}; | |
template<typename L1, typename L2> | |
auto make_combined(L1&& l1, L2 && l2) { | |
return S<std::decay_t<L1>, std::decay_t<L2>>(std::forward<L1>(l1), std::forward<L2>(l2)); | |
} | |
int main() | |
{ | |
auto l1 = []() {return 10; }; | |
auto l2 = [](int x) {return x*x; }; | |
// auto combined = S(l1, l2); // this compile in gcc7 using c++17 class template deduction | |
auto combined = S<decltype(l1), decltype(l2)>(l1, l2); | |
auto combined2 = make_combined(l1, l2); | |
return combined(10) + combined() + combined2(20) + combined2(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment