2016-04-05 15 views
0

Ich versuche, den Größencode für das Cbor-Format mit "SFINAE" zu implementieren, aus Mangel an einem besseren Wort. Aber es funktioniert nicht, wie zum Beispiel size_code<3>0x1b auswertet. Was ist los mit dir?Variable Vorlage "SFINAE" funktioniert nicht

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

Es sollte nicht alles bewerten. Dieser Code ist fehlerhaft für die mehrfache Neudefinition von 'size_code'. –

+0

Sie wären wahrscheinlich besser dran, eine "constexpr" -Funktion dafür zu schreiben. – TartanLlama

+0

@ T.C. Gcc Kuriosität, aber Clang erkennt dies. – user1095108

Antwort

3

Sie können variable Vorlagen nicht so definieren, daher sollte Ihr Code nicht funktionieren.

viel einfacher mit einer constexpr Funktion, so etwas wie dies Dies wäre:

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

Meine 2 Cent:

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; 
Verwandte Themen