Boost logo

Boost :

Subject: Re: [boost] [convert] no-throw and fallback feature
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-05-09 17:39:11

Vicente BOTET wrote:
> De : "Stewart, Robert"
> > Vicente BOTET wrote:
> > > If we see optional as a kind of conditional tuple and we
> > > imagine something like a conditional tie (let me call it
> > > 'ctie'), we could be able to write
> > >
> > > ctie(i,b) = try_convert_to(s);
> ctie will left i unchanged if b is true.

ctie won't change i if the conversion succeeds?

> > I presume that no assignment would be done to i if b is
> > false.

Surely that is the right interpretation.

> > That actually means writing this:
> >
> > int i;
> > bool b;
> > ctie(i, b) = try_converting_to(s);
> > if (!b)
> > {
> > i = fallback;
> > std::cerr << "using fallback\n";
> > }
> >
> > Compare that to this:
> >
> > int i(fallback);
> > if (!try_converting_to(s, i))
> > {
> > std::cerr << "using fallback\n";
> > }
> The fist is more on the line of functional paradigm. The second
> on the procedural one. I understand that the procedural one can
> be more efficient, but the functional one separates clearly
> what are the in a,d the out parameters. Are both alternatives
> exclusive?

They aren't exclusive, but they'd need different names and, again, I suspect we'd be getting into the too-many-choices conundrum.

> > Besides, the same thing is done with the following which is
> > simpler and less tricky:
> >
> > int i;
> > if (try_converting_to(s, i)) ...
> If conversion to bool is implicit the bool parameter will be
> redundant and the preceding could be expressed as
> int i;
> if ((opt_tie(i) = try_convert_to(*int*)(s))) ...

Ah, right. That's a curious approach to it. You're suggesting, if I have all the parts aligned right, that try_convert_to() return optional<T> and that opt_tie() assign to its argument iff the optional is set. That means there's an optional-based API and the following is also possible:

int i(fallback);
opt_tie(i) = try_convert_to<int>(s);

Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

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