Boost logo

Boost :

Subject: Re: [boost] [convert] Default Syntax Idea
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2009-02-25 09:36:36

On Wednesday, February 25, 2009 4:44 AM
Vladimir Batov wrote:
> From: <Vladimir.Batov_at_[hidden]>
> >> From: "Stewart, Robert" <Robert.Stewart_at_[hidden]>
> >>
> >> int const i(3);
> >> string const s("something");
> >> int const j(convert<int>(s) | 5);
> >
> > So, I am voting 'yes'... in principle.
> Now I am not so sure. In isolation it looks good:
> int j = convert_to<int>(s) | 5;
> What happens when we start mixing with
> int j = convert_to<int>(s) | 5 >> std::hex;
> int j = convert_to<int>(s) | 5 >> boost::dothow >> std::hex;
> or even
> int j = convert_to<int>(s) >> boost::dothow >> std::hex | 5;

I don't think the throw/nothrow part belongs, but it's still good for illustrating your concern with manipulators.

| has a lower precedence than >> (or <<), so those expressions won't work. They'd have to be written as:

   (convert<int>(s) | 5) >> std::hex;

Is that bad? Yes. Many will be frustrated by how easy it is to forget the parentheses and get a compilation error when supplying the default. The alternative to an operator is some named default function or an extra, confusing argument:

   convert<int>(s).or_(5) >> std::hex;
   convert<int>(s, 5) >> std::hex;

To avoid the precedence problem, the available operators that make any sense at all are:

   convert<int>(s)[5] >> std::hex;
   convert<int>(s) ! 5 >> std::hex;
   convert<int>(s) ~ 5 >> std::hex;
   convert<int>(s) & 5 >> std::hex;
   convert<int>(s) / 5 >> std::hex;
   convert<int>(s) % 5 >> std::hex;

The subscript operator is not bad. "[5]" implies that 5 is optional in a number of contexts. Bang suggests using 5 on error. Tilde doesn't evoke "default" in any way, but it also doesn't look like an egregious misuse of the operator. The final three just look wrong to me.

Given that the default value calls should be easy to get right and that the precedence issue will bite many, we should select a different operator if we choose to retain an operator for the job. Among the available choices, I favor bang:

   convert<int>(s) ! 5 >> std::hex;

Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
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