【发布时间】:2016-04-05 08:19:27
【问题描述】:
我正在尝试使用“SFINAE”实现 cbor 格式的尺寸代码,因为没有更好的词。但它不起作用,例如,size_code<3> 的计算结果为 0x1b。怎么了?
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;
【问题讨论】:
-
它不应该评估任何东西。此代码格式错误,无法多重重新定义
size_code。 -
您最好为此编写一个
constexpr函数。 -
@T.C. gcc 很奇怪,但 clang 意识到了这一点。
-
我知道 constexpr 函数和带有静态 constexpr 成员解决方法的结构,但如果可能的话,我真的很想避免这种情况
-
@TartanLlama 我已取消删除。您可以发布您的答案,我会接受。