2016-04-05 21 views
0

Daha iyi bir kelime olmaması nedeniyle "SFINAE" ile cbor formatı için beden kodunu uygulamaya çalışıyorum. Ancak, size_code<3> gibi, çalışmaz, 0x1b olarak değerlendirir. Sorun nedir? kodunuz çalışmaya olmamalıdır böylecedeğişken şablon "SFINAE" çalışmıyor

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

O şey değerlendirmek gerekir. Bu kod, 'size_code' değerini yeniden tanımlamak için kötü biçimlendirilmiştir. –

+0

Bunun için bir "constexpr" işlevi yazmanız daha iyi olurdu. – TartanLlama

+0

@ T.C. gcc tuhaflığı, ama clang bunu tanır. – user1095108

cevap

3

Sen böyle değişken şablonları yeniden tanımlamak mümkün değil.

Bu

böyle bir constexpr fonksiyonu, bir şey ile çok daha basit olacaktır:

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

Benim 2 sent:

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; 
İlgili konular