开发者

Trying to understand a GCC error

I have the following bit of code:

#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;
}
开发者_如何转开发

when I compile it as follows:

g++ -pedantic -ansi -Wall -Werror -O2 -o  test test.cpp

I get the following error:

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

This error is seen with O3, but not with O1. I've compiled the code using the comeau online compiler, MS VC9.0 and icc v11 and in all cases the code compiles without an issue.

The code works fine with std::vector, std::deque, std::set, char*, int* iterators, seems to be something very specific to the implementation of std::list.

I was hoping someone could provide some insight into what this particular error(warning) means and how to go about resolving it.

Note: The GCC version is:

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.


I've replicated your error on gcc-4.4. This is a non-error with gcc-4.5 and gcc-4.6, from Debian "unstable."

I've experienced a somewhat related bug specific to gcc-4.4, similar to this filed bug: Bug 42488 – [4.4 only] spurious strict-aliasing warning. As @michael-burr pointed out, there are a large number of "strict-aliasing rules" bugs against gcc-4.4: GCC Bug List: strict aliasing rules 4.4.

I realize it's a bit unsatisfying, but I've come to accept this as a bug in GCC 4.4, and been able to move onto newer versions that don't have this problem.


No clu 'bout gnu ... but this seems useful : http://code.google.com/p/v8/issues/detail?id=413


Just hazarding a guess: if you declared the args of foo to be const, it may fix this issue. The "aliasing" issue, if I understand it correctly, comes up when more than one pointer in a loop could be pointing to the same data - this leads to potential order of operation bugs with some optimizations (which is what the warning is referring to).


Here's a bugzilla case that seems to represent this problem or something very similar:

  • http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38937

It's marked as fixed in 4.4.0, so you've either run into another corner case, or ???. But this bug might give you a leg up on a resolution for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜