Boost logo

Boost Users :

Subject: [Boost-users] can't extend boost::any on windows
From: Michael Linck (mgl_at_[hidden])
Date: 2008-12-16 13:38:54


Hi, I've implemented a simple, low level json formatting utility to
complement tinyjson's lightweight parser. To do this, I've extended
boost::any only to add a "format()" function to it that is aware of what
types the content can take in a json structure and produces the
appropriate string. The default constructor and the constructor with a
value are the only things I implemented, aside from the format
function. Both just call the base constructor. Compiled in gcc,
everything works fabulously. Apparently, a functioning assignment
operator and copy constructor are appropriately inherited and everything
runs great. On windows, however, some subtle difference causes
assignment and copies to seg-fault. The real problem is that, for the
life of me, I can't find a way to implement assignment or copies
explicitly, that doesn't also bomb out. I can probably come up with a
work around that stores my type in shared pointers so they're never
actually copied, but I'm really curious if anyone could explain what
causes this. Here are some code snippets to illustrate the behavior:

void test(){
    std::cout<<"messing around with boost
any"<<std::endl;
    boost::any
tAny;
    boost::any tStringAny(std::string("some
string"));
    tAny =
tStringAny;
    std::cout<<"string any assignable to
any"<<std::endl;
                                                                                

    std::cout<<"messing around with value
type"<<std::endl;
    sysshep::json::Value
tValue;
    std::cout<<"formatted blank value
"<<tValue.format()<<std::endl;
    std::cout<<"making string
value"<<std::endl;
                                                                                

    sysshep::json::Value
tStringValue(std::string("some_string"));
    std::cout<<"formatted string value
"<<tStringValue.format()<<std::endl;
    std::cout<<"assigning string value to blank
value"<<std::endl;
    tValue =
tStringValue; /// FAILS
    std::cout<<"formatted blank value after
assignment"<<tValue.format()<<std::endl;
}

class Value : public boost::any
{
    public:
       Value():boost::any(){};

       template<typename ValueTypeTA>
          Value(const ValueTypeTA & aValue):
          : boost::any(aValue) {}
   
        virtual ~Value();

        std::string format() const;
};

I'm not going to list the different ways I've attempted to implement
copy and assignment, some more plausible than others, out of respect for
brevity. If you try a build on windows, VC8, you should be able to mess
around with this to your hearts content. It's really annoying because
it was a factor in the fundamental elegance of the parsing
implementation, because I see no reason why it shouldn't work, because
it worked fine in linux, and because it put my windows port at least a
day behind customer expectations.

thanks for any insight.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net