On Oct 4, 8:05 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Ranganath wrote:
On Oct 3, 8:46 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Ranganath wrote:
Why is throw keyword considered as an operator?
It is impossible to answer a question based on a wrong premise.
'throw' is not an operator. Why do you think it is one? Or,
rather, what do you mean "considered as an operator"? What is
the context?
throw is an operator.
According to the language standard, "throw" is a keyword, not an operator.
As far as I can see, the language standard doesn't define
operators. Without a definition, I'm incapable of saying
whether throw is an operator or not.
Please have a look at clauses [2.11] (listing all keywords) and [2.12]
(listing all operators and punctuators).
Please read the first sentence of §2.12:
The lexical representation of C++ programs includes a number
of preprocessing tokens which are used in the syntax of the
preprocessor or are converted into tokens for operators and
punctuators:"
Note the relevance with regards to the preprocessor. The fact
that throw isn't in this list means that:
#define throw catch
is legal (unless you include a standard header, in which case,
§17.4.3.1.1/2 applies: "A translation unit shall not #define or
#undef names lexically identical to keywords").
That is, in fact, all it means.
One may, of course, wonder about the conditions for inclusion in
this list. There's really no more reason for new to be in it,
than for throw. I suspect that it's more or less historical.
K&R (at least the original edition) had two lists, one for
operators, and one for punctuation (and the comma was in both).
I think Stroustrup originally followed this (although I don't
have my copy of his book handy to verify), adding new and delete
to the list of operators, because they can appear in an
expression. The C++ commitee dropped the distinction, adopting
rather the distinction pre-processor/parser, and merged the two
lists---logically, I suppose, new and delete should have been
dropped from the resulting list at that time. And the alternate
tokens got added---necessarily, since the pre-processor has to
recognize them. Throw got added to the language much later
(than new and delete), and since there was no need for the
preprocessor to know about it, it didn't get added to the list.
At any rate, there's no real logic behind it: the pre-processor
has no need to treat new and delete differently from throw, or
even while; on the other hand, it does treat the keywords true
and false differently (although they aren't in the list).
(IIRC, as late as CD2, there was no special treatment of true or
false by the pre-processor. Which meant that "#if true"
evaluated to "#if 0", and the following block would not be
compiled!)
Please have a look athttp://en.wikipedia.or g/wiki/Operators_in_C_ and_C++.
That would be a either a bug in wikipedia or may indicate that
wikipedia uses a different notion of "operator" (maybe
something that aims to be meaningful across programming
languages, but I am just guessing here).
All I wanted to know
is what constraint makes it to be treated as an operator.
Huh?
I think what he's really asking for is the definition of
operator. IMHO, there isn't really one, and asking whether
"throw" (or "while", or "new") are operators or not is a
meaningless question. (And the Wikipedia is a wonderful
resource for meaningless or unimportant questions---fun
questions, in other words---, even if it isn't very good as a
reference or for serious work.) I suspect that the definition
being used here is that an operator is a keyword or punctuation
that can be used in an expression (but then, what is the second
"int" in "int i = int(d);").
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34