Skip to content

Instantly share code, notes, and snippets.

@aruslan
Created July 5, 2016 15:10
Show Gist options
  • Save aruslan/2e1bbe0b2832f11087306ef78a2d8666 to your computer and use it in GitHub Desktop.
Save aruslan/2e1bbe0b2832f11087306ef78a2d8666 to your computer and use it in GitHub Desktop.
For Pavel Ilin
#include <utility>
template <typename T>
auto func(T& t) -> decltype(std::declval<T>().method(), bool())
{
return t.method();
}
bool func(int& i) { return 42; }
struct SOk { bool method(); };
struct SFail {};
template<typename T>
constexpr auto funcIsOk(int) -> decltype(func(std::declval<T&>()), bool()) { return true; }
template<typename T>
constexpr auto funcIsOk(...) -> bool { return false; }
int main()
{
int i = 42;
func(i); // ok
SOk sok;
func(sok); // ok
SFail sfail;
//func(sfail); // compile error
static_assert(funcIsOk<SOk>(0), "SOk should be ok");
static_assert(!funcIsOk<SFail>(0), "SFail should fail");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment