2016-04-05 6 views
0

より良い単語がないため、「SFINAE」のコーバーフォーマットのサイズコードを実装しようとしています。しかし、たとえば、size_code<3>0x1bと評価されているため、機能しません。どうしましたか?可変テンプレート「SFINAE」が機能しない

template <::std::size_t N, 
    typename = ::std::enable_if_t<N <= 0x17> 
> 
constexpr ::std::uint8_t const size_code = N; 

template <::std::size_t N, 
    typename = ::std::enable_if_t<(N > 0x17) && 
    (N <= ::std::numeric_limits<::std::uint8_t>::max()) 
    > 
> 
constexpr ::std::uint8_t const size_code = 0x18; 

template <::std::size_t N, 
    typename = ::std::enable_if_t< 
    (N > ::std::numeric_limits<::std::uint8_t>::max()) && 
    (N <= ::std::numeric_limits<::std::uint16_t>::max()) 
    > 
> 
constexpr ::std::uint8_t const size_code = 0x19; 

template <::std::size_t N, 
    typename = ::std::enable_if_t< 
    (N > ::std::numeric_limits<::std::uint16_t>::max()) && 
    (N <= ::std::numeric_limits<::std::uint32_t>::max()) 
    > 
> 
constexpr ::std::uint8_t const size_code = 0x1a; 

template <::std::size_t N, 
    typename = ::std::enable_if_t< 
    (N > ::std::numeric_limits<::std::uint32_t>::max()) && 
    (N <= ::std::numeric_limits<::std::uint64_t>::max()) 
    > 
> 
constexpr ::std::uint8_t const size_code = 0x1b; 
+0

は、それが何に評価されてはなりません。このコードは、 'size_code'を何度も再定義するためのものです。 –

+0

このためには、おそらく 'constexpr'関数を書いたほうがよいでしょう。 – TartanLlama

+0

@ T.C。奇妙なgccですが、clangはこれを認識します。 – user1095108

答えて

3

このような変数テンプレートを再定義することはできないため、コードは機能しません。

これは、このような何かconstexpr機能とはるかに簡単になります:

template <typename T> constexpr T t_max = std::numeric_limits<T>::max(); 

constexpr std::uint8_t size_code (std::size_t n) { 
    if (n <= 0x17) return n; 
    if (n <= t_max<std::uint8_t>) return 0x18; 
    if (n <= t_max<std::uint16_t>) return 0x19; 
    if (n <= t_max<std::uint32_t>) return 0x1a; 
    if (n <= t_max<std::uint64_t>) return 0x1b; 
} 
1

私の2セント:

template <::std::size_t N, typename = void> 
constexpr ::std::uint8_t const size_code{}; 

template <::std::size_t N> 
constexpr ::std::uint8_t const size_code<N, ::std::enable_if_t<N <= 0x17> > = N; 

template <::std::size_t N> 
constexpr ::std::uint8_t const size_code<N, 
    ::std::enable_if_t<(N > 0x17) && 
    (N <= ::std::numeric_limits<::std::uint8_t>::max()) 
    > 
> = 0x18; 

template <::std::size_t N> 
constexpr ::std::uint8_t const size_code<N, 
    ::std::enable_if_t< 
    (N > ::std::numeric_limits<::std::uint8_t>::max()) && 
    (N <= ::std::numeric_limits<::std::uint16_t>::max()) 
    > 
> = 0x19; 

template <::std::size_t N> 
constexpr ::std::uint8_t const size_code<N, 
    ::std::enable_if_t< 
    (N > ::std::numeric_limits<::std::uint16_t>::max()) && 
    (N <= ::std::numeric_limits<::std::uint32_t>::max()) 
    > 
> = 0x1a; 

template <::std::size_t N> 
constexpr ::std::uint8_t const size_code<N, 
    ::std::enable_if_t< 
    (N > ::std::numeric_limits<::std::uint32_t>::max()) && 
    (N <= ::std::numeric_limits<::std::uint64_t>::max()) 
    > 
> = 0x1b; 
関連する問題