我有以下代码:
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
template<typename Iterator>
void foo(Iterator begin, Iterator end)
{
typedef typename std::iterator_traits<Iterator>::value_type type;
type smallest = (*std::min_element(begin,end));
std::cout << smallest << std::endl;
}
int main()
{
std::list<int> l;
l.push_back(1);
l.push_back(2);
foo(l.begin(),l.end());
return 0;
}
当我编译时如下:
g++ -pedantic -ansi -Wall -Werror -O2 -o test test.cpp
我收到以下错误:
cc1plus: warnings being treated as errors
In function ‘int main()’:
cc1plus: error: dereferencing pointer ‘pretmp.163’ does break strict-aliasing rules
cc1plus: note: initialized from here
这个错误在 O3 身上出现,但在 O1 身上却没有。我已经使用 comeau 在线编译器、MS VC9.0 和 icc v11 编译了代码,在所有情况下代码编译都没有问题。
该代码适用于 std::vector
、std::deque
、std::set
、char*
,int*
迭代器,似乎是非常特定于 std::list 实现的东西。
我希望有人能提供一些关于这个特定错误(警告)的含义以及如何解决它的见解。
注意:GCC版本为:
gcc (Ubuntu 4.4.1-4ubuntu9) 4.4.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
最佳答案
我已经在 gcc-4.4 上复制了你的错误。这是来自 Debian“unstable”的 gcc-4.5 和 gcc-4.6 的非错误。
我遇到了一个与 gcc-4.4 相关的错误,类似于这个提交的错误:Bug 42488 – [4.4 only] spurious strict-aliasing warning .正如@michael-burr 所指出的,存在大量针对 gcc-4.4 的“严格别名规则”错误:GCC Bug List: strict aliasing rules 4.4 .
我意识到这有点不令人满意,但我已经接受这是 GCC 4.4 中的一个错误,并且能够转移到没有这个问题的较新版本。
关于c++ - 试图了解 GCC 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2114378/