While working on a toy project, I began to answer the SO question, which flooded me with a g ++ warning, which I do not understand.
format.hpp:230: warning: dereferencing pointer '<anonymous>' does break strict-aliasing rules
Internet search I got the impression that this might be a g ++ error; is this really a mistake, and if so, is there a workaround? The full source code is too large to include, but is available here . Here is the part where the warning is triggered ...
template<typename T> class ValueWrapper : public ValueWrapperBase { public: T x; ValueWrapper(const T& x) : x(x) {} virtual std::string toString(const Field& field) const { return Formatter<T>().toString(x, field); } private:
Line 230 is p->second = vw; .
I get a warning for each instance of the operator() template method, always on line 230.
EDIT
Apparently, the error is due to the use of map iterators, which can generate inline code, which the optimizer confuses. Rewriting the section, avoiding the use of iterators, I got shorter code, which is also easy to compile without warning.
template<typename T> Dict& operator()(const std::string& name, const T& value) { ValueWrapperBase *vw = new ValueWrapper<T>(value); ValueWrapperBase *& p(env[name]); delete p; p = vw; return *this; }
source share