Skip to content

Instantly share code, notes, and snippets.

@rbock
Created April 2, 2016 11:24
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 rbock/45e01aee17b29e239efed21b1d6bdd15 to your computer and use it in GitHub Desktop.
Save rbock/45e01aee17b29e239efed21b1d6bdd15 to your computer and use it in GitHub Desktop.
#include <type_traits>
#include <utility>
template<typename T>
struct wrong
{
static constexpr auto value = false;
};
struct not_integral
{
template <typename T = void>
static void _()
{
static_assert(wrong<T>::value, "parameter for test has to be an integral value");
}
};
struct ok
{
};
template<typename Check>
struct bad_statement
{
bad_statement()
{
Check::_();
}
};
template<typename Check>
using is_ok = std::enable_if_t<std::is_same<Check, ok>::value>;
template<typename Check>
using is_not_ok = std::enable_if_t<not std::is_same<Check, ok>::value>;
template <typename T>
using check = std::conditional_t<std::is_integral<T>::value,
ok,
bad_statement<not_integral>>;
/*
* TEST FUNCTION AND ITS IMPLEMENTATION
* ------------------------------------
*/
template<typename Check, typename T, typename = is_ok<Check>>
auto test_impl(const Check&, T t) -> void
{
// Do something
return;
};
template<typename Check, typename T, typename = is_not_ok<Check>>
auto test_impl(const Check&, T t) -> Check;
template <typename T>
auto test(T t) -> decltype(test_impl(check<T>{}, t))
{
return test_impl(check<T>{}, t);
};
/*
* MAIN
* ------------------------------------
*/
int main()
{
static_assert(std::is_same<void, decltype(test(7))>::value, "");
static_assert(std::is_same<bad_statement<not_integral>, decltype(test(""))>::value, "");
test("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment