Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does boost::optional fail for classes inheriting virtual functions

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 ?

like image 899
Martin Avatar asked Oct 10 '22 16:10

Martin


1 Answers

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 {};
like image 146
Akira Takahashi Avatar answered Oct 19 '22 23:10

Akira Takahashi