この質問の直接的な解決策は次のようになります。
template< typename T, T X, T L, T H>
using inside_t =
std::enable_if_t< (X <= H) && (X >= L),
std::integral_constant<T, X> >;
OPに適用:
template<typename C, unsigned K> struct X; // final {};
template<unsigned K>
struct X<char, K> final
{
using ascii_ordinal = inside_t<unsigned, K, 0, 127>;
char value = char(ascii_ordinal::value);
};
それが仕事をするとき、これは本当にひどいCLエラーメッセージをレンダリングします:
X<char, 300> a; //here 300 is out of range and I would like to be able to detect that.
あまり洗練されていませんが、最も快適なAPIは次のようになります。
template<unsigned K>
struct X<char, K> final
{
static_assert( K >= 0U && K <= 127U, "\n\nTeribly sorry, but value must be between 0 and 127 inclusive\n\n") ;
char value = char(K);
};