5

我正在对可变参数进行一些实验,但偶然发现了一个我无法弄清楚解决方案的问题 - 基本上我正在尝试用任意数据类型的组件构建一棵树 - 这是一些代码:

template <class A, class B>
struct SeqExpression
{
    const A & first;
    const B & then;
};

template <class A, class B>
SeqExpression<A,B>
make_seq(const A & a, const B & b)
{
    return {a,b};
}

template <class A, class B, class ...T>
auto
make_seq(const A & first, const B & second, T ...rest) -> decltype(make_seq(make_seq(first,second),rest...))
{

    return make_seq(make_seq(first,second),rest...);
}

然后我尝试:

auto x = make_seq("X","Y",'z');

但是 GCC(4.7) 告诉我:

error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template<class A, class B, class ... T> decltype (make_seq(make_seq(first, second), rest ...)) make_seq(const A&, const B&, T ...) [with A = SeqExpression<char [2], char [2]>; B = char; T = {}]’
recursively required by substitution of ‘template<class A, class B, class ... T> decltype (make_seq(make_seq(first, second), rest ...)) make_seq(const A&, const B&, T ...) [with A = SeqExpression<char [2], char [2]>; B = char; T = {}]’
required by substitution of ‘template<class A, class B, class ... T> decltype (make_seq(make_seq(first, second), rest ...)) make_seq(const A&, const B&, T ...) [with A = char [2]; B = char [2]; T = {char}]’

在我看来,它应该是可以解决的!

make_seq("X","Y")有类型SeqExpression< char[2],char[2] > 所以make_seq(make_seq("X","Y"),'z')有类型SeqExpression< SeqExpression< char[2],char[2] >,char >

对我来说,它似乎相对没有循环。

有什么想法吗?

4

1 回答 1

3

问题是当您有两个参数(在两个参数模板上)时,您的可变参数模板被选中。您需要确保仅当您至少有三个参数时才选择它;最简单的方法是添加另一个参数:

template <class A, class B, class C, class ...T>
auto
make_seq(const A & first, const B & second, const C &third, T ...rest)
 -> decltype(make_seq(make_seq(first,second), third, rest...))
{

    return make_seq(make_seq(first,second), third, rest...);
}

可变参数包可以并且将匹配零个参数;这可能看起来令人惊讶,但它比替代方案更好。


请注意,上面显然利用了返回类型的非标准 g++ 扩展。以下类型程序应该能够计算对应于参数序列的类型:

template <int n, class ...T> struct mst_helper;
template <class A, class B>
struct mst_helper<2, A, B> { typedef SeqExpression<A, B> type; };
template <int n, class A, class B, class ...T>
struct mst_helper<n, A, B, T...> {
    typedef typename mst_helper<n - 1, SeqExpression<A, B>, T...>::type type; };
template <class ...T>
struct make_seq_type { typedef typename mst_helper<sizeof...(T), T...>::type type; };

template <class A, class B, class C, class ...T>
typename make_seq_type<A, B, C, T...>::type
make_seq(const A & first, const B & second, const C &third, T ...rest)
{
    return make_seq(make_seq(first,second), third, rest...);
}

g++-4.7.1 似乎有模板别名的错误,所以我不得不使用struct而不是using.

于 2012-08-17T11:57:27.287 回答