Boost logo

Boost :

From: (noreply_at_[hidden])
Date: 2004-03-15 22:44:41

Bugs item #917070, was opened at 2004-03-15 21:44
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting:

Category: lexical_cast
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: John F Meinel Jr (jfmeinel)
Assigned to: Kevlin Henney (kevlin)
Summary: Bug in lexical_cast.hpp

Initial Comment:
In boost-1_31_0, I believe there is a typo in

The line
    Target lexical_cast(Source arg)
should be
    Target lexical_cast(Source& arg)

Otherwise it actually creates a copy of the argument
before doing the lexical cast.
This causes problems if you try to lexical_cast() a
virtual object.
Here is code that shows the bug. By just adding the
'&', the code performs as you would expect it to.

#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>

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_cast<std::string>(c);
  std::cout << s << std::endl; //This should print 2
  s = boost::lexical_cast<std::string, B>(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<std::string, A>(c);
  std::cout << s << std::endl;


You can respond by visiting:

This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
Boost-bugs mailing list

Boost list run by bdawes at, gregod at, cpdaniel at, john at