Skip to content

Instantly share code, notes, and snippets.

@rolandschulz
Last active January 28, 2019 07:02
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 rolandschulz/3ca4e0773f82f108a845be3af02ddc5f to your computer and use it in GitHub Desktop.
Save rolandschulz/3ca4e0773f82f108a845be3af02ddc5f to your computer and use it in GitHub Desktop.
Checking whether member function is constexpr
//Checking whether member function is constexpr
#include <array>
#include <vector>
template<typename T, typename = void>
struct has_constexpr_size : std::false_type {};
template<typename T, typename Fn, typename ...Args>
constexpr auto invoke_memfn(Fn fn, Args...args) { return (T().*fn)(args...); }
template<typename T>
#ifndef _MSC_VER
struct has_constexpr_size<T, std::enable_if_t<(T().size(),true)>> : std::true_type {};
#else
struct has_constexpr_size<T, std::enable_if_t<(invoke_memfn<T>(&T::size),true)>> : std::true_type {};
#endif
template<typename T>
constexpr bool has_constexpr_size_v = has_constexpr_size<T>::value;
static_assert(has_constexpr_size_v<std::array<int, 5>>,"");
static_assert(!has_constexpr_size_v<std::vector<int>>,"");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment