Boost logo

Boost :

From: SourceForge.net (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:
https://sourceforge.net/tracker/?func=detail&atid=107586&aid=917070&group_id=7586

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
lexical_cast.hpp

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:
https://sourceforge.net/tracker/?func=detail&atid=107586&aid=917070&group_id=7586

-------------------------------------------------------
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
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
_______________________________________________
Boost-bugs mailing list
Boost-bugs_at_[hidden]
https://lists.sourceforge.net/lists/listinfo/boost-bugs


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk