Created
January 31, 2019 18:35
-
-
Save devhawk/c54b26f1d84fafc30b4f494d0ffbc050 to your computer and use it in GitHub Desktop.
C++ Helper function to get nullptr constructed instance if nullptr ctor is available, default constructed instance if nullptr ctor is not available
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 T, typename = std::void_t<>> | |
| struct empty_instance | |
| { | |
| static T get() { return T{}; } | |
| }; | |
| template <typename T> | |
| struct empty_instance<T, std::void_t<decltype(T{ nullptr })>> | |
| { | |
| static T get() { return T{ nullptr }; } | |
| }; | |
| template <typename T> | |
| T get_empty_instance() | |
| { | |
| return empty_instance<T>::get(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment