I picked this up in one of my brief forays to reddit:
http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/
Basically, the author points out that in C++:
throw "error"
is an expression. This is actually fairly clearly spelt out in the C++ Standard, both in the main text and the grammar. However, what is not clear (to me at least) is what is the type of the expression? I guessed "void
", but a bit of experimenting with g++ 4.4.0 and Comeau yielded this code:
void f() {
}
struct S {};
int main() {
int x = 1;
const char * p1 = x == 1 ? "foo" : throw S(); // 1
const char * p2 = x == 1 ? "foo" : f(); // 2
}
The compilers had no problem with //1 but barfed on //2 because the the types in the conditional operator are different. So the type of a throw
expression does not seem to be void.
So what is it?
If you answer, please back up your statements with quotes from the Standard.
This turned out not to be so much about the type of a throw expression as how the conditional operator deals with throw expressions - something I certainly didn't know about before today. Thanks to all who replied, but particularly to David Thornley.