Created
July 5, 2016 15:10
-
-
Save aruslan/2e1bbe0b2832f11087306ef78a2d8666 to your computer and use it in GitHub Desktop.
For Pavel Ilin
This file contains 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 <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