【问题标题】:Odd behavior when recursively building a return type for variadic functions递归构建可变参数函数的返回类型时的奇怪行为
【发布时间】:2010-04-19 08:39:18
【问题描述】:

这可能是一个非常简单的解释,但我会尽可能多地提供背景故事,以防我错了。为如此冗长而深表歉意。我正在使用 gcc4.5,我意识到对 c++0x 的支持仍处于实验阶段,但我将假设我所看到的行为与错误无关的原因。

我正在试验可变参数函数模板。最终目标是从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...))>

我的困惑来自于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;
}

非结构解决方案:

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;
}

产生的错误:

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&)'

结构解:

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;
}

产生的错误:

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>'

【问题讨论】:

  • 暂时无法尝试,但可能是延迟返回类型扣除仍然失败。在第二种情况下,您应该能够 typedef 返回类型:typedef std::pair&lt;Type, typename BuildStruct&lt;Types...&gt;::result_type&gt; result_type;(并且适合第一个专业化)并使用它们:static result_type Go(...);
  • 天哪,这很多要读:)
  • 您的“非结构解决方案”代码与我的 GCC 版本(4.4.3)编译得很好。我找不到该代码有任何问题,但我听说 GCC4.5 中有几个部分排序缺陷。这似乎是其中之一(它可能会失败,因为对于“const char,const char”的情况,它错误地选择了可变参数模板,然后会尝试依次调用没有参数的 BuildFunction - 因此一直失败)
  • 迹象表明这是特定版本的 gcc 中的一个非常局部的错误。所以我投票决定关闭,因为它过于本地化。
  • 对于一个需要计算机科学学位才能理解的问题的解释并不简单。

标签: c++ templates c++11 variadic-functions


【解决方案1】:

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

【讨论】:

  • 我一个月前得出了这个结论,感谢您的回答,这样我就可以让它消失了:)
猜你喜欢
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
  • 2011-04-07
  • 2020-06-18
相关资源
最近更新 更多