boost::optional<> works perfect for simple data types but as soon as used for a class inheriting from a class implementing an interface it fails when strict aliasing is enabled.
Example:
#include <boost/optional.hpp>
struct MyLine{
double a;
double b;
};
class Edge{
public:
MyLine toMyLine() const;
private:
virtual MyLine doToMyLine() const =0;
};
class Wall:public Edge {
public:
Wall(MyLine const& seg):mMyLine(seg){};
private:
MyLine doToMyLine() const{return MyLine();};
MyLine mMyLine;
};
class SimpleWall {
public:
SimpleWall(MyLine const& seg):mMyLine(seg){};
private:
MyLine mMyLine;
};
int main(){
//boost::optional<Wall> res; //fails with strict aliasing error
boost::optional<SimpleWall> res2; //compiles just fine
}
Compiled with the following using gcc version 4.4.3 this is on error:
g++ -c -pipe -Wall -Wextra -Wunused -Wmissing-declarations -Wpointer-arith -Wcast-align -Wwrite-strings -Wredundant-decls -Werror -std=c++0x -O2 -Wall -W -I/usr/local/boost_1_44_0 -o obj/main.o main.cpp
What is the best way to solve this problem. I would very much like to leave strict-aliasing warning enabled.I'm using boost version 1.44.
UPDATE:
It gets worse!! Consider the following code:
#include <boost/optional.hpp>
class MyBase{
public:
int toFoo() const;
private:
virtual int doToFoo() const =0;
};
class Child:public MyBase {
public:
Child(int const& foo):mFoo(foo){};
private:
int doToFoo() const{return 0;}
int mFoo;
};
int main(){
boost::optional<int> optint; //comment out for surprise
optint.get(); //comment out for surprise
boost::optional<Child> res2;
res2.get();
}
Compiled with the following using gcc version 4.4.3 this compiles:
g++ -c -pipe -Wall -Wextra -Wunused -Wmissing-declarations -Wpointer-arith -Wcast-align -Wwrite-strings -Wredundant-decls -Werror -std=c++0x -O2 -Wall -W -I/usr/local/boost_1_44_0 -o obj/main.o main.cpp
If the lines marked with "//comment out for surprise" are commented out, I get a strict aliasing warning. I have checked this at least 20 times. This is among the weirdest things I ever saw. Looks like boost::optional initializes sth. independent from its template parameter or like gcc getting to understand boost::optional only if called with sth. trivial first. Any ideas ?
I tried that program in Boost 1.44.0. This problem reason is don't override doToSegment.
Segment doToSegment(){};
should be add const:
Segment doToSegment() const {};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With