I'd like to check a value at compile time, if it's a constexpr
value and do run a constexpr
form-checking function on it if it is.
Can I do this in c++11/14?
In pseudo code, I'd like to do:
static_if (is_constexpr(x))
static_assert(check(x))
Details:
I'm checking a string for a particular format. It's conceptually:
template<std::size_t N>
constexpr bool check(const char (&s)[N]){ return true; }
//^ really a recursive call that checks the string
The constexpr check works with a string literal, but not with a string pointer:
int main(){
const char* s = "#";
static_assert(check("#"), "Fmt");; //OK
//This fails; expectation was it suceeds and the check doesn't run
static_assert(!is_constexpr(s) || check(s), "Fmt");
}
The is_constexpr
is:
#include <type_traits>
template<typename T>
constexpr typename std::remove_reference<T>::type makeprval(T && t) {
return t;
}
#define is_constexpr(e) noexcept(makeprval(e))