递归构建可变参数函数的返回类型时的奇怪行为

Den*_*ose 23 c++ templates variadic-functions c++11

这可能是一个非常简单的解释,但是如果我错了,我会尽可能多地给出背景故事.对于如此冗长而道歉.我正在使用gcc4.5,并且我意识到c ++ 0x支持仍然有点实验性,但我将假设我所看到的行为存在非bug相关的原因.

我正在尝试使用可变参数函数模板.最终目标是建立一个缺点std::pair.它不是一个自定义类型,只是一对配对对象.构造列表的函数必须以某种方式递归,最终返回值取决于递归调用的结果.作为附加扭曲,连续参数在插入列表之前被添加在一起.所以如果我通过[1,2,3,4,5,6],最终结果应该是{1 + 2,{3 + 4,5 + 6}}.

我最初的尝试相当天真.一个函数,Build,有两个重载.一个人拿了两个相同的参数,然后简单地返回它们 另一个采用了两个参数和一个参数包.返回值是一对由两个设置参数和递归调用之和组成.回想起来,这显然是一个有缺陷的策略,因为当我试图找出它的返回类型时,函数没有被声明,所以它别无选择,只能解析为非递归版本.

我明白了 我困惑的地方是第二次迭代.我决定让这些函数成为模板类的静态成员.函数调用本身不是参数化的,而是整个类.我的假设是,当递归函数尝试生成其返回类型时,它将使用自己的静态函数实例化整个新版本的结构,并且所有内容都可以自行运行.

结果是:"错误:没有匹配函数来调用BuildStruct<double, double, char, char>::Go(const char&, const char&)"

违规代码:

static auto Go(const Type& t0, const Type& t1, const Types&... rest)
    -> std::pair<Type, decltype(BuildStruct<Types...>::Go(rest...))>
Run Code Online (Sandbox Code Playgroud)

我的困惑来自于这样的事实:参数BuildStruct应始终与发送的参数类型相同BuildStruct::Go,但在错误代码Go中缺少最初的两个双参数.我在这里错过了什么?如果我最初关于如何选择静态函数的假设是不正确的,为什么它试图调用错误的函数而不是根本找不到函数?它似乎只是混合类型,而且我无法想出为什么.如果我在初始调用中添加其他参数,它总是在失败之前向下挖掘到最后一步,因此假设递归本身至少部分工作.这与初始尝试形成鲜明对比,初始尝试始终无法立即找到函数调用.

最终,我已经解决了这个问题,一个相当优雅的解决方案,几乎不像前两次尝试.所以我知道如何做我想做的事.我正在寻找我所看到的失败的解释.

完整的代码,因为我确信我的口头描述不足.首先是一些样板文件,如果您觉得有必要执行代码并亲自查看它.然后是合理失败的初始尝试,然后是第二次尝试,但没有.

#include <iostream>
using std::cout;
using std::endl;

#include <utility>

template<typename T1, typename T2>
std::ostream& operator <<(std::ostream& str, const std::pair<T1, T2>& p) {
  return str << "[" << p.first << ", " << p.second << "]";
}

//Insert code here    

int main() {
  Execute(5, 6, 4.3, 2.2, 'c', 'd');
  Execute(5, 6, 4.3, 2.2);
  Execute(5, 6);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

非结构解决方案:

template<typename Type>
Type BuildFunction(const Type& t0, const Type& t1) {
  return t0 + t1;
}

template<typename Type, typename... Rest>
auto BuildFunction(const Type& t0, const Type& t1, const Rest&... rest)
      -> std::pair<Type, decltype(BuildFunction(rest...))> {
  return std::pair<Type, decltype(BuildFunction(rest...))>
                  (t0 + t1, BuildFunction(rest...));
}

template<typename... Types>
void Execute(const Types&... t) {
  cout << BuildFunction(t...) << endl;
}
Run Code Online (Sandbox Code Playgroud)

产生的错误:

test.cpp: In function 'void Execute(const Types& ...) [with Types = {int, int, double, double, char, char}]':
test.cpp:33:35:   instantiated from here
test.cpp:28:3: error: no matching function for call to 'BuildFunction(const int&, const int&, const double&, const double&, const char&, const char&)'
Run Code Online (Sandbox Code Playgroud)

结构解决方案

template<typename... Types>
struct BuildStruct;

template<typename Type>
struct BuildStruct<Type, Type> {
  static Type Go(const Type& t0, const Type& t1) { return t0 + t1; }
};

template<typename Type, typename... Types>
struct BuildStruct<Type, Type, Types...> {
  static auto Go(const Type& t0, const Type& t1, const Types&... rest)
        -> std::pair<Type, decltype(BuildStruct<Types...>::Go(rest...))> {
    return std::pair<Type, decltype(BuildStruct<Types...>::Go(rest...))>
               (t0 + t1, BuildStruct<Types...>::Go(rest...));
  }
};

template<typename... Types>
void Execute(const Types&... t) {
  cout << BuildStruct<Types...>::Go(t...) << endl;
}
Run Code Online (Sandbox Code Playgroud)

产生的错误:

test.cpp: In instantiation of 'BuildStruct<int, int, double, double, char, char>':
test.cpp:33:3:   instantiated from 'void Execute(const Types& ...) [with Types = {int, int, double, double, char, char}]'
test.cpp:38:41:   instantiated from here
test.cpp:24:15: error: no matching function for call to 'BuildStruct<double, double, char, char>::Go(const char&, const char&)'
test.cpp:24:15: note: candidate is: static std::pair<Type, decltype (BuildStruct<Types ...>::Go(BuildStruct<Type, Type, Types ...>::Go::rest ...))> BuildStruct<Type, Type, Types ...>::Go(const Type&, const Type&, const Types& ...) [with Type = double, Types = {char, char}, decltype (BuildStruct<Types ...>::Go(BuildStruct<Type, Type, Types ...>::Go::rest ...)) = char]
test.cpp: In function 'void Execute(const Types& ...) [with Types = {int, int, double, double, char, char}]':
test.cpp:38:41:   instantiated from here
test.cpp:33:3: error: 'Go' is not a member of 'BuildStruct<int, int, double, double, char, char>'
Run Code Online (Sandbox Code Playgroud)

bma*_*ies 2

阅读评论,似乎很清楚这是 G++ 特定版本中的一个非常本地化的错误,这就是所有的答案。