I found that when I call boost::lexical_caststd::string() with an
object that has virtual functions, it fails to dereference the virtual
functions. It also cannot use an Abstract base type. Attached is example
code.
I think the problem is simply that the file boost/lexical_cast.hpp, on
line 185 has:
Target lexical_cast(Source arg)
This should be
Target lexical_cast(Source& arg)
At least, this fixes all of my problems.
John
=:->
#include <iostream>
#include <string>
#include
struct A { virtual int f() const = 0; };
struct B : A { virtual int f() const { return 1; } };
struct C : B { virtual int f() const { return 2; } };
std::ostream& operator<<(std::ostream& os, const A& a) {
return os << a.f();
}
int main()
{
C c; std::string s;
std::cout << c << std::endl; //This should print 2
s = boost::lexical_caststd::string(c);
std::cout << s << std::endl; //This should print 2
s = boost::lexical_cast(c);
std::cout << s << std::endl; //This returns 1, not 2
//This fails because it tries to instantiate an object of type A. Why???
s = boost::lexical_cast(c);
std::cout << s << std::endl;
}