使用SFINAE根据包的大小启用部分特化

问题描述 投票:3回答:4

我正在尝试在编译时制作一个适用于字符的模板。在这种情况下,我想强加一个约束,即必须始终存在一定数量的字符的精确倍数。

在没有完全匹配的情况下,我想在包的头部用0填充它们。

(顺便说一下,这背后的动机是希望(在编译时,作为一个更大的问题的一部分)添加支持将二进制和十六进制文字映射到std::array<unsigned char, N>,这可以很好地工作,除了填充不是多个的东西字节)。

这是一个简化的例子,说明我要做的是让padding工作:

// Thingy operates on N*4 chars - if that's not met use inheritance to 0 pad until it is met.
template<char ...Args>
struct thingy : thingy<0, Args...> {
    // All we do here is recursively add one more 0 via inheritance until the N*4 rule is met
};

// This specialisation does the real work, N=1 case only
template<char a, char b, char c, char d>
struct thingy<a,b,c,d> {
    enum { value = (a << 24) | (b << 16) | (c << 8) | d }; 
};

// This handles chunking the N*4 things into N cases of work. Does work along the way, only allowed for exact N*4 after padding has happened.
template <char a, char b, char c, char d, char ...Args>
struct thingy<a,b,c,d,Args...> : thingy<a,b,c,d> {
    static_assert(sizeof...(Args) % 4 == 0); // PROBLEM: this is a we're a better match than the template that pads things, how do we stop that?
    // Do something with the value we just got and/or the tail as needed
    typedef thingy<a,b,c,d> head;
    typedef thingy<Args...> tail;
};

int main() {
    thingy<1,1,1,1,1>(); // This should be equivalent to writing thingy<0,0,0,1,1,1,1,1>()
}

这击中了我的static_assert。问题是我们总是匹配错误的专业化,这是我期待的,因为它更专业。


所以我环顾四周,找到了同样问题for a function的一些例子,但据我所知,这些都不适用于此。

我尝试了一些更多的东西,其中没有一个像我希望的那样工作,首先是在enable_if天真的sizeof...(Args)到底我想要的地方:

template <char a, char b, char c, char d, typename std::enable_if<sizeof...(Args) % 4 == 0, char>::type ...Args>
struct thingy<a,b,c,d,Args...> : thingy<a,b,c,d> {
    // ...
};

这是不合法的,虽然据我所知,当然不能在我的编译器上工作 - 在我们需要查询sizeof...(Args)时,Args尚不存在。

据我所知,我们无法合法地在包装后添加另一个模板参数,这也失败了:

template <char a, char b, char c, char d, char ...Args, typename std::enable_if<sizeof...(Args) % 4 == 0, int>::type=0>
struct thingy<a,b,c,d,Args...> : thingy<a,b,c,d> {
    // ...
};

有错误:

pad_params_try3.cc:17:8: error: default template arguments may not be used in partial specializations

我还在继承中尝试了SFINAE,但这似乎不是一个合法的地方:

template <char a, char b, char c, char d, char ...Args>
struct thingy<a,b,c,d,Args...> : std::enable_if<sizeof...(Args) % 4 == 0, thingy<a,b,c,d>>::type {
    // ...
};

在那,我们击中了static_assert和失败,这是enable_if的错误。

pad_params_try4.cc:17:8: error: no type named 'type' in 'struct std::enable_if<false, thingy<'\001', '\001', '\001', '\001'> >'
 struct thingy<a,b,c,d,Args...> : std::enable_if<sizeof...(Args) % 4 == 0, thingy<a,b,c,d>>::type {
        ^~~~~~~~~~~~~~~~~~~~~~~
pad_params_try4.cc:18:5: error: static assertion failed
     static_assert(sizeof...(Args) % 4 == 0);

据我所知,我可以通过阅读更多的this might even be considered a defect,但这对我现在没什么帮助。

我如何使用C ++ 14,gcc 6.x中的内容解决这个问题?有没有比完全回到绘图板更简单的选择?

c++ c++14 template-meta-programming sfinae
4个回答
2
投票

如何使用多重继承的稍微不同的方法和执行填充的中间帮助器结构?

// This handles chunking the N*4 things into N cases of work. Does work along the way, only allowed for exact N*4 after padding has happened.
template <char a, char b, char c, char d, char... Args>
struct thingy_impl : thingy_impl<a, b, c, d>, thingy_impl<Args...> {
    static_assert(sizeof...(Args) % 4 == 0);
    // Do something with the value we just got and/or the tail as needed
    typedef thingy_impl<a,b,c,d> head;
    typedef thingy_impl<Args...> tail;
};

template<char a, char b, char c, char d>
struct thingy_impl<a,b,c,d> {
    enum { value = (a << 24) | (b << 16) | (c << 8) | d }; 
};

template<int REMAINDER, char... Args>
struct padding;

template<char... Args>
struct padding<0,Args...> { using type = thingy_impl<Args...>; };

template<char... Args>
struct padding<1,Args...> { using type = thingy_impl<0,0,0,Args...>; };

template<char... Args>
struct padding<2,Args...> { using type = thingy_impl<0,0,Args...>; };

template<char... Args>
struct padding<3,Args...> { using type = thingy_impl<0,Args...>; };

template<char... Args>
struct thingy : padding<sizeof...(Args) % 4, Args...>::type { };

int main() {
    thingy<1,1,1,1,1>(); // This should be equivalent to writing thingy<0,0,0,1,1,1,1,1>()
}

Demo, with a diagnostic output.


2
投票

首先,一个简单的C ++解决方案17,使用递归的if constexpr辅助函数为你做填充:

template<char ... Args>
auto getThingyPadded()
{
    if constexpr (sizeof...(Args) % 4 != 0)
        return getThingyPadded<0, Args...>();
    else
        return thingy<Args...>{};
}

为了制作这个C ++ 14,我们需要使用SFINAE而不是if constexpr。我们可以为我们添加一个计算sizeof...(Args)的调用来规避你所描述的问题:

template<bool B, class U = void>
using enableIfT = typename std::enable_if<B, U>::type;

template<std::size_t N, enableIfT<(N % 4 == 0)>* = nullptr, char ... Args>
auto getThingyPaddedHelper()
{
    return thingy<Args...>{};
}

template<std::size_t N, enableIfT<(N % 4 != 0)>* = nullptr, char ... Args>
auto getThingyPaddedHelper()
{
    return getThingyPaddedHelper<N+1, nullptr, 0, Args...>();
}

template<char ... Args>
auto getThingyPadded()
{
    return getThingyPaddedHelper<sizeof...(Args), nullptr, Args...>();
}

Demo!


1
投票

我将摆脱头/尾的列表并使用std::tuple,结果:

// No variadic here
template <char a, char b, char c, char d>
struct thingy {
    enum { value = (a << 24) | (b << 16) | (c << 8) | d }; 
};

template <typename Seq, char... Cs>
struct thingies_impl;

template <std::size_t ...Is, char... Cs>
struct thingies_impl<std::index_sequence<Is...>, Cs...>
{
private:
    static constexpr char get(std::size_t n)
    {
        constexpr char cs[] = {Cs...};
        constexpr std::size_t paddingSize = (4 - (sizeof...(Cs) % 4)) % 4;
        return (n < paddingSize) ? '\0' : cs[n - paddingSize];
    }

public:
    using type = std::tuple<thingy<get(4 * Is),
                                   get(4 * Is + 1),
                                   get(4 * Is + 2),
                                   get(4 * Is + 3)>...>;  
};

template <char... Cs>
using thingies = thingies_impl<std::make_index_sequence<(sizeof...(Cs) + 3) / 4>, Cs...>;

Demo


0
投票

另一种多重继承方法(通过Jarod42的修正和简化(谢谢!))。

#include <utility>

template <char a, char b, char c, char d, char ... Args>
struct t_base : public t_base<Args...>
 {
   typedef t_base<a,b,c,d> head;
   typedef t_base<Args...> tail;
 };

template <char a, char b, char c, char d>
struct t_base<a, b, c, d>
 { enum { value = (a << 24) | (b << 16) | (c << 8) | d }; };


template <typename, typename>
struct t_helper;

template <std::size_t ... Is, char ... Cs>
struct t_helper<std::index_sequence<Is...>,
                std::integer_sequence<char, Cs...>>
   : public t_base<(Is, '0')..., Cs...>
 { };


template <char ... Cs>
struct thingy :
   public t_helper<std::make_index_sequence<(4u - sizeof...(Cs) % 4u) % 4u>,
                   std::integer_sequence<char, Cs...>>
 { };


int main ()
 {
 }
© www.soinside.com 2019 - 2024. All rights reserved.