Skip to content

Instantly share code, notes, and snippets.

@devhawk
Created January 31, 2019 18:35
Show Gist options
  • Select an option

  • Save devhawk/c54b26f1d84fafc30b4f494d0ffbc050 to your computer and use it in GitHub Desktop.

Select an option

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
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