Created
September 15, 2017 20:41
-
-
Save mtao/a6a4d13333eb481e5453a9e7cb2ac137 to your computer and use it in GitHub Desktop.
Code to try multiple attempts at creating a single object
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
| #include <iostream> | |
| template <typename F, typename... Fs> | |
| auto call_until_not_nullptr(F&& f, Fs&&... fs) -> decltype(f()) { | |
| if(auto v = f()) { | |
| return v; | |
| } else { | |
| if constexpr(sizeof...(fs) != 0) { | |
| return call_until_not_nullptr(std::forward<Fs>(fs)...); | |
| } else { | |
| return nullptr; | |
| } | |
| } | |
| } | |
| int main(int argc, char * argv[]) { | |
| int a = 0; | |
| call_until_not_nullptr( | |
| [&a]() -> int* { | |
| std::cout << "Returning a ptr" << std::endl; | |
| return &a; | |
| } | |
| , | |
| []() -> int* { | |
| std::cout << "Returning nullptr" << std::endl; | |
| return nullptr; | |
| } | |
| ); | |
| std::cout << "Calls a few times" << std::endl; | |
| std::cout << "=================" << std::endl; | |
| call_until_not_nullptr( | |
| []() -> int* { | |
| std::cout << "Returning nullptr" << std::endl; | |
| return nullptr; | |
| } | |
| , | |
| []() -> int* { | |
| std::cout << "Returning nullptr" << std::endl; | |
| return nullptr; | |
| } | |
| , | |
| []() -> int* { | |
| std::cout << "Returning nullptr" << std::endl; | |
| return nullptr; | |
| } | |
| , | |
| [&a]() -> int* { | |
| std::cout << "Returning a ptr" << std::endl; | |
| return &a; | |
| } | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment