Skip to content

Instantly share code, notes, and snippets.

@RedBeard0531
Created May 5, 2016 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RedBeard0531/ac115a4c881a3205d63feada4910487f to your computer and use it in GitHub Desktop.
Save RedBeard0531/ac115a4c881a3205d63feada4910487f to your computer and use it in GitHub Desktop.
Compile fail test
#include <type_traits>
/**
* Test that 'Code' fails to compile.
* 'Type' is aliased to 'Alias' and it must be used in 'Code'
*/
#define COMPILE_FAIL_TEST(Name, Type, Alias, Code) \
struct Name { \
template <typename Alias> \
static auto compiles(Alias && ) -> decltype((void)(Code), std::true_type()); \
static auto compiles(...) -> std::false_type; \
static_assert(!decltype(compiles(std::declval<Type>()))(), \
"Compile test failure: " #Name); \
}
/**
* Test that 'Code' compiles (dual of COMPILE_FAIL_TEST).
*/
#define COMPILE_WORK_TEST(Name, Type, Alias, Code) \
auto Name = [] { \
using Alias = Type; \
(void)(Code); \
}
struct Foo {
Foo(int) {}
};
COMPILE_FAIL_TEST(ConstructWithStr, Foo, T, T("100"));
COMPILE_WORK_TEST(ConstructWithInt, Foo, T, T(100));
int main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment