Last active
January 28, 2019 07:02
-
-
Save rolandschulz/3ca4e0773f82f108a845be3af02ddc5f to your computer and use it in GitHub Desktop.
Checking whether member function is constexpr
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
//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