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.