Skip to content

Instantly share code, notes, and snippets.

@nikhedonia
Created November 1, 2015 20:19
Show Gist options
  • Save nikhedonia/4c49413ece653421f28f to your computer and use it in GitHub Desktop.
Save nikhedonia/4c49413ece653421f28f to your computer and use it in GitHub Desktop.
#include <type_traits>
#include <iostream>
using namespace std;
auto Valid = [](auto...x) -> integral_constant<bool,1> {};
template<class F,class...X>
constexpr auto is_callable(F f,X...x)
-> decltype( f(x...), true_type{} ) {
return{};
}
constexpr auto is_callable(...)
-> false_type {
return{};
}
template<class F, class X>
auto Fail(F f,X x)
-> enable_if_t<!is_callable(f,x),true_type> {
return{};
}
template<class F, class...X>
auto Check(F f, X...x) -> decltype( Valid( f(x)...) ){}
auto Not = [](auto f){
return [f](auto x){
return Fail(f,x);
};
};
auto hasX = [](auto x) -> decltype( x.x(), 0 ) {};
template<class T>
auto doSth(T x) -> decltype(
Check( hasX(x) ) , 1
) {
return 1;
}
template<class T>
auto doSth(T x) -> decltype(
Check( Not(hasX)(x) ) , 1
) {
return 0;
}
struct test {
void x(){}
};
int main() {
cout<< doSth(1) << doSth(test{}) << endl; //01
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment