constexpr 允许在编译时评估的表达式在编译时进行评估.
为什么这个关键字是必要的?为什么不允许或要求编译器在编译时评估所有表达式?
标准库有一个不均匀的constexpr应用程序,这会带来很多不便.使constexpr成为"默认"将解决这个问题,并可能改进大量现有代码.
我想检查一个类型是否在std :: numeric_limits中有一个条目.当类型是数组 - (或者可能不是数字?)时,我得到编译器错误.这使我无法根据std :: numeric_limits中是否支持该类型来检测和分支.我很感激任何想要分享的见解.
// the following provokes compiler error on Clang
// Function cannot return array type 'type' (aka 'char [20]')
static_assert(
! std::numeric_limits<char[20]>::is_specialized,
"! std::numeric_limits<char[20]>::is_specialized"
);
// invokes static assert on compile as expected
static_assert(
std::numeric_limits<char[20]>::is_specialized,
"std::numeric_limits<char[20]>::is_specialized"
);
Run Code Online (Sandbox Code Playgroud) 以下代码说明了我的问题
#include <type_traits>
#include <limits>
#include <cstdint>
#include <boost/mpl/if.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/identity.hpp>
/////////////////////////////////////////////////////////////////
// safe_signed_range
template <
std::intmax_t MIN,
std::intmax_t MAX
>
struct safe_signed_range {
};
/////////////////////////////////////////////////////////////////
// safe_unsigned_range
template <
std::uintmax_t MIN,
std::uintmax_t MAX
>
struct safe_unsigned_range {
};
template<class T, class U>
using calculate_max_t = typename boost::mpl::if_c<
std::numeric_limits<T>::is_signed
|| std::numeric_limits<U>::is_signed,
std::intmax_t,
std::uintmax_t
>::type;
template<typename T, typename U>
struct test {
typedef calculate_max_t<T, U> max_t;
static_assert(std::is_same<max_t, std::intmax_t>::value, "unexpected value for max_t");
static_assert(std::is_signed<max_t>::value, "check parameter");
/*
typedef typename boost::mpl::if_c< …Run Code Online (Sandbox Code Playgroud)