Boost logo

Boost :

From: Sebastien Marc (sebmarc_at_[hidden])
Date: 2001-10-17 16:37:20


Thanks for the clarification Steve.
I guess i read the article too quickly. My fault. For
some reason, i skipped the assignment:
multiA = multiB;
from the code and believed the convertion was from an
any containing a long into a const char *.

Thanks for your prototype too.

Fernando is right. I cannot expect a convertion to
const char* to work because of the memory allocation
issues, and thanks for William for the clarification.
lexical_cast<string> will just do what i want.

I suspect other people will be tempted to naively cast
an any containing a long into a const char* using
any_cast until they realize that is not possible.

Thanks for your responses.

Seb.

--- scleary_at_[hidden] wrote:
> > Yes steve, i understood that.
> >
> > What is not clear to me is that an exception is
> > thrown. I would have expected the convertion, the
> > actual cast from any, containing 5, to const char
> *)
> > to work.
> >
> > To me, the resulting string s should contain "5"
> but
> > instead the exception is thown.
>
> Oh. Sorry, I misunderstood your question.
>
> Take a look at the beginning of the "any" docs at
> Kevlin's three points
> under "Motivation". "any" preserves type
> differences (i.e., does *not*
> provide automatic conversions). Conversions like
> what you want are supplied
> by boost::lexical_cast.
>
> There doesn't exist a class taking advantage of
> lexical_cast like you want
> (right now, anyway -- maybe you could submit one?).
> Here's a simple
> prototype for one: (warning: untested code :)
>
> class variant
> {
> private:
> std::string data;
> public:
> template <typename T>
> variant(const T & other)
> :data(boost::lexical_cast<std::string>(other)) {
> }
> template <typename T>
> variant & operator=(const T & other)
> {
> data =
> boost::lexical_cast<std::string>(other);
> return *this;
> }
> // Possibly add "operator T()" ???
> template <typename T>
> T as()
> { return boost::lexical_cast<T>(data); }
> };
>
> variant any_value;
> any_value = 5;
> std::string s = any_value.as<std::string>();
>
> -Steve
>
> Info: http://www.boost.org Unsubscribe:
> <mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to
> http://docs.yahoo.com/info/terms/
>
>

__________________________________________________
Do You Yahoo!?
Make a great connection at Yahoo! Personals.
http://personals.yahoo.com


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