Skip to content

Instantly share code, notes, and snippets.

@hutorny
Created December 16, 2023 16:49
Show Gist options
  • Save hutorny/907e858743ae69835aca3bcdd40c7baa to your computer and use it in GitHub Desktop.
Save hutorny/907e858743ae69835aca3bcdd40c7baa to your computer and use it in GitHub Desktop.
is_constexpr implementation for function returning structural type (integral or enumeration)
#include <type_traits>
// is_constexpr implementation
// Live example https://godbolt.org/z/79adTjdax
template<auto F>
struct constexpr_true : std::true_type {};
template<auto F>
static auto test_constexpr(int) -> constexpr_true<F()>;
template<auto F>
static auto test_constexpr(long) -> std::false_type;
template<auto F>
struct is_constexpr : decltype(test_constexpr<F>(0)) {};
// is_constexpr test
struct A {
static constexpr int c() {
return 1;
}
};
struct B {
static int c() noexcept {
return cc;
}
static inline int cc = 1;
};
static_assert(is_constexpr<&A::c>::value);
static_assert(!is_constexpr<&B::c>::value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment