Re: throw operator
On Oct 3, 7:16 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Erik Wikstr=F6m wrote:
On 2007-10-03 12:31, Ranganath wrote:
Why is throw keyword considered as an operator?
For the same reason that new, delete, and sizeof are operators I would
suspect. What else would it be, if it was not an operator?
What's 'return'? 'throw' is exactly that.
Not really. Throw can be used in an expression, return can't.
'delete' is an operator because you're allowed to overload it and
because the name of that function would be 'operator delete'. But
among the rest of true operators it stands out because it does not
yield a value and hence cannot be used in another expression.
You're forgetting the comma operator, and ?:. Things like:
MyClass::MyClass( T arg )
: myMember( isValid( arg ) ? arg : throw "Invalid arg")
{
}
are perfectly valid and legal.
'throw' is a keyword used in two constructs: to declare the list
of exceptions the function can throw and to actually redirect the
flow. 'return' isn't used to declare anything, neither is 'goto'.
But 'throw' is much more like 'return' or 'goto' than 'sizeof' or
'typeid'.
Throw can be used to introduce an exception specificaion of a
function declaration, in which case, it isn't an operator, or it
can be used in an expression, in which case, it is. Except, of
course, that this distinctio between operators and other tokens
is rather superficial and meaningless. The only significant
distinction made by the standard is between pre-processor
op-or-punc (which includes new and delete, but not throw and
return), and the actual language tokens. The only real
difference between new and throw (or new and while) is that:
#define new 0 // Illegal, always...
#define throw 0 // Legal, unless you include a
// standard header.
For the rest, after preprocessing, both are tokens, and both are
keywords. There is no distiction between operators and
non-operators, at least in the standard. In common parlance, I
think that you'd consider something an operator if it is used in
an expression, and "punctuation" otherwise. In which case,
"throw" is like a comma: sometimes one, sometimes the other.
But then, so is int: it can introduce a declaration, but it can
also be used as a conversion operator.
In sum, the only real answer is that there is no such
distinction operator/non-operator, or that any such distinction
is arbitrary, and decided upon by the author.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34