Created
October 21, 2015 16:01
-
-
Save Masstronaut/63b73b1d9e7dd43b5325 to your computer and use it in GitHub Desktop.
Easy to use macro for creating SFINAE checks.
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 <type_traits> | |
// Create our interface | |
// usage: T1 is the typename, x1 is the variable name. | |
#define DEFINE_SFINAE_CHECK( NAME, EXPR ) \ | |
template <typename U1> \ | |
struct NAME \ | |
{ \ | |
using yes = char; \ | |
using no = char[2]; \ | |
template <typename T1>\ | |
static no& f1(...);\ | |
template<typename T1> \ | |
static auto f1(T1 x1) -> decltype( (EXPR), yes() ); \ | |
enum { value = sizeof(NAME::f1<U1>((std::declval<U1>(), U1()))) \ | |
== sizeof(NAME::yes) \ | |
}; \ | |
} | |
// make a SFINAE check called is_ptr that checks if *x1 is a valid expression. | |
DEFINE_SFINAE_CHECK(is_ptr, *x1); | |
#include <iostream> | |
int main() | |
{ | |
static_assert(is_ptr<int *>::value == true, "int * should be a pointer type."); | |
static_assert(is_ptr<int>::value == false, "int should not be a pointer type."); | |
std::cout << "Success!\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment