In C++ throw is an expression
July 31st, 2009
9 comments
Erythromycin Online
Buy Coumadin
Penisole Online
Buy Phentrimine
Zelnorm Online
Buy Elavil
Flomax Online
Buy Aldactone
Avapro Online
Buy Zelnorm
After 15 years programming in C++, I was surprised to discover today that throw in C++ is an expression rather than a statement. As a result, throw may be used as part of larger expressions.
int x = 5; int y = x > 4 ? x : throw std::out_of_range;
The only use I’ve found for this — and in fact the speculative attempt by which I discovered it — is range checking within constructor initializer lists.
RangeChecked::RangeChecked(int x) :
int_member(x > 0 ? x : throw std::out_of_range)
{
}
It’s academic what the type of a throw expression is, since it will never be returned, but for type-checking purposes the compiler seems to be happy to use it in place of any type.

Recent Comments