Skip to content

Instantly share code, notes, and snippets.

@neontorrent
Last active January 4, 2019 18:52
Show Gist options
  • Save neontorrent/216fd1bea9c8bb5a593214ad8d87ddae to your computer and use it in GitHub Desktop.
Save neontorrent/216fd1bea9c8bb5a593214ad8d87ddae to your computer and use it in GitHub Desktop.
Boilerplates for C++
//Template Type Check
template<typename T, typename F,
typename = std::enable_if_t<std::is_invocable<F, T>::value> >
void foo()
{}
template<typename T, typename F,
typename = typename std::enable_if<std::is_invocable<F, T>::value>::type >
void foo()
{}
template<typename T, typename F,
typename std::enable_if<std::is_invocable<F, T>::value>::type* = nullptr >
void foo()
{}
template<typename T, typename F>
auto foo() -> std::enable_if_t<std::is_invocable<F, T>::value>::type
{}
template<typename T, typename F>
void foo()
{
static_assert(std::is_invocable<F, T>::value);
}
@neontorrent
Copy link
Author

Ad-hoc polymorphism with templated type can be achieved using:

  1. std::enable_if_t<std::is_xxxx::value>
  2. std::void_t<decltype(expression)>
  3. C++2a concept

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment