Topic: Proposal: change to condition ? value1 : value2


Author: "Greg Brewer" <nospam.greg@brewer.net>
Date: Thu, 22 Feb 2001 21:10:51 GMT
Raw View
I would like to see the
   condition ? value1 : value 2
construct changed to the value1 is optional.  If not given the value of the
condition is used instead.

For an example, allow
   const char *comma = strchr(input, ',')?:"";
so that comma now either points to the first comma in input or to a null
string.

It's fairly easy to get around this but I find that I frequently have to
save funtion output into a temporary variable, test for a bad return, assign
a corrective value, then continue.  Perhaps a better example would be

void Foo::Bar1(char *input)
{
      Bar2(strchr(input,',')?:defaultvalue);
}

In this case, strchr finds the next comma and passes either that or
Foo::defaultvalue to the Bar2 function.

Probably just another dumb idea but ...
Greg




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: qrczak@knm.org.pl (Marcin 'Qrczak' Kowalczyk)
Date: Thu, 22 Feb 2001 22:46:07 GMT
Raw View
Thu, 22 Feb 2001 21:10:51 GMT, Greg Brewer <nospam.greg@brewer.net> pisze=
:

> I would like to see the
>    condition ? value1 : value 2
> construct changed to the value1 is optional.  If not given the value of=
 the
> condition is used instead.

gcc has this extension.

--=20
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZAST=CAPCZA
QRCZAK

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: gt5163b@prism.gatech.edu (Brian McNamara!)
Date: Thu, 22 Feb 2001 23:32:53 GMT
Raw View
"Greg Brewer" <nospam.greg@brewer.net> once said:
>I would like to see the
>   condition ? value1 : value 2
>construct changed to the value1 is optional.  If not given the value of the
>condition is used instead.
>
>For an example, allow
>   const char *comma = strchr(input, ',')?:"";
>so that comma now either points to the first comma in input or to a null
>string.
>
>It's fairly easy to get around this but I find that I frequently have to
>save funtion output into a temporary variable, test for a bad return, assign
>a corrective value, then continue.  Perhaps a better example would be
>
>void Foo::Bar1(char *input)
>{
>      Bar2(strchr(input,',')?:defaultvalue);
>}
>
>In this case, strchr finds the next comma and passes either that or
>Foo::defaultvalue to the Bar2 function.
>
>Probably just another dumb idea but ...

Does something like this suffice?

   template <class T>
   T ifElseDefault( T value, T def) {
      return value ? value : def ;
   }

   const char *comma = ifElseDefault( strchr(input, ','), "");

It seems to me a function provides the same abstraction without any need
to extend the language.

(Although, if the default value is an expression with side-effects that
shouldn't be evaluated if the condition is true, then indeed, the
function above is insufficient.  Indeed, I can't even find a solution
with a macro.)

--
 Brian M. McNamara   lorgon@acm.org  :  I am a parsing fool!
   ** Reduce - Reuse - Recycle **    :  (Where's my medication? ;) )

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Greg Brewer" <nospam.greg@brewer.net>
Date: Mon, 26 Feb 2001 22:58:56 GMT
Raw View
> Does something like this suffice?
>
>    template <class T>
>    T ifElseDefault( T value, T def) {
>       return value ? value : def ;
>    }
>
>    const char *comma = ifElseDefault( strchr(input, ','), "");

No.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Anon" <spamme@lots.UCAR.EDU>
Date: Mon, 26 Feb 2001 23:00:18 GMT
Raw View
Greg Brewer <nospam.greg@brewer.net> wrote in message
news:973sk5$2c06$1@news.hal-pc.org...
> I would like to see the
>    condition ? value1 : value 2
> construct changed to the value1 is optional.  If not given the value of
the
> condition is used instead.
>
> For an example, allow
>    const char *comma = strchr(input, ',')?:"";
> so that comma now either points to the first comma in input or to a null
> string.

This sounds both useful and elegant to me. I wish my compiler had this
extension.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Deja User" <gennaro_prota@my-deja.com>
Date: Mon, 26 Feb 2001 23:07:14 GMT
Raw View
On Thu, 22 Feb 2001 23:32:53 GMT, gt5163b@prism.gatech.edu (Brian McNamara!) wrote:


>Does something like this suffice?
>
>   template <class T>
>   T ifElseDefault( T value, T def) {
>      return value ? value : def ;
>   }
>
>   const char *comma = ifElseDefault( strchr(input, ','), "");
>
>It seems to me a function provides the same abstraction without any need
>to extend the language.
>
>(Although, if the default value is an expression with side-effects that
>shouldn't be evaluated if the condition is true, then indeed, the
>function above is insufficient.  Indeed, I can't even find a solution
>with a macro.)
>

Hehe... :-)))  So it seems that the extension Greg Brewer proposes makes sense!

Just a note: it is natural, in my opinion, that:

  cond? : expr2;

should not evaluate cond twice, even if cond is true; this is different from

cond? cond : expr2,


For instance,

    const char* input = "This, has, commas";
    const char *comma = strchr(input, ',')?   :   "";

should cause at the most one call of strchr.

[I refer to the proposed extension for the ?: operator... i'm not saying that your function
template produces a double evaluation, it's obvious that one evaluation of the
argument used to initialize the parameter 'value' occurs :-)  ]

What do you think about it?

     Gennaro Prota

------------------------------------------------------------
--== Sent via Deja.com ==--
http://www.deja.com/


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Ken Hagan" <K.Hagan@thermoteknix.co.uk>
Date: Wed, 28 Feb 2001 10:59:56 CST
Raw View
I asked, "isn't this what the logical OR gives us?"

"Barry Margolin" <barmar@genuity.net> wrote...
>
> No, because that always coerces the result to a boolean,
> returning either 1 or 0.

Ah yes, silly me. We'd need...

    (/*something*/ = cond) || (/*something*/ = expr2);

...but this can't be used as a initialising expression for
"something".

    T const something = (.... ? .... : ....);

Besides, using || purely for its short-circuit evaluation
property (rather than its result) is slightly evil.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]