【问题标题】:std::stack corrupts return valuestd::stack 损坏返回值
【发布时间】:2010-11-01 18:18:48
【问题描述】:

为了说明我的问题,我已将代码简化为以下内容:

#include <iostream>
#include <stack>
#include <utility>

std::pair<double,double> test(double a, double b)
{
    std::stack<int> my_stack;
    return std::make_pair<double,double>(a,b);
}

int main()
{
    std::pair<double,double> p = test(1.1,2.2);
    std::cout << p.first << " " << p.second << "\n";

    return 0;
}

当我使用 gcc -O1 标志时,来自 test() 函数的返回值被破坏。这是一些示例输出:

$ gcc -O2 a.cxx  -lstdc++
$ ./a.out
1.1 2.2
$ gcc -O1 a.cxx  -lstdc++
$ ./a.out
2.60831e-317 2.60657e-317
$ gcc -v
Reading specs from /usr/lib64/gcc-lib/x86_64-suse-linux/3.3.3/specs
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-    prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --enable-languages=c,c++,f77,objc,java,ada --disable-checking --libdir=/usr/lib64 --enable-libgcj --with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib64 --with-system-zlib --enable-shared --enable-__cxa_atexit x86_64-suse-linux Thread model: posix
gcc version 3.3.3 (SuSE Linux)

此代码适用于除“-O1”之外的所有 gcc 优化标志。如果我删除 my_stack 的声明,它也可以工作。您会将其归类为编译器错误,还是我缺少有关 std::stack 和返回 std::pair 值的内容?

【问题讨论】:

  • 顺便说一句,make_pair 的全部意义在于指定模板参数,让它们由函数参数推导。
  • 换句话说,你可以简单地写return std::make_pair(a,b)。由于ab 都是双打,它会正常工作 (tm)

标签: c++ optimization gcc std-pair


【解决方案1】:

绝对是编译器错误。 顺便说一句,我的 GCC 版本(4.4.5)没有复制它。

SuSE 9.1 附带的 GCC 3.3.3 版本已知已损坏(请参阅 here)。

【讨论】:

  • gcc 4.3.4 20090804 (release) 1 也没有复制。
【解决方案2】:

这是一个错误。检查是否已报告该错误,如果没有,请报告。

【讨论】:

  • 记下他正在使用一个古老版本的 gcc
猜你喜欢
  • 2015-08-01
  • 2020-02-26
  • 1970-01-01
  • 1970-01-01
  • 2020-01-01
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多