On Thu, 21 Aug 2003 13:40:18 +0300, Attila Feher wrote:
Nils Petter Vaskinn wrote: On Thu, 21 Aug 2003 11:19:41 +0100, John Harrison wrote:
Its not allowed. Any expression has a single type, which can be
deduced at compile time by the compiler. Your suggestion would mean
that an expressions type could not be determined until a program runs.
Sounds like a nightmare to me (and not only for compiler writers).
And how would that be harder than compiling
Yes, it is. Your example can be decided at *compile* time.
But not which foo() to call.
Is there any reason that ?: with different types is impossible and/or a
bad idea,
Impossible.
Now that is plain unbelievable. If you said incredibly hard I would
believe you. If you said Impossible without rewriting the internals of the
compiler completely, I would believe you. But impossible is very rarely
the case.
except for compiler writers having to deal with it? Does the standard
state WHY it isn't allowed.
I am not going to look up the standard now. In C++ (well, why cannot
people read a decent C++ introductory book such as Accelerated C++
before starting to be critics? - sorry) expressions have a type. A
Type. One Type. And expression cannot have two types => be two things.
I did look ?: up in The C++ Programming Language and it provided very
little explanation. I also looked up the behaviour of ?: in K&R which
provided a little more detail and explained that the operands would have
to be the same type (unless one could be cast automatically to the other).
What I never found was any reason why this limitation exists in C++ (which
is much more dynamic than C), except for the "It's hard" explanation.
I didn't criticise anything that I'm aware of, I was asking "why is it
this way". (So that later I might descide to criticise if the explanation
doesn't make sense to me)
Now you have explained that it violates a principle (an expression has one
and only one type) I accept that. There never really was a problem since I
know how to do without ?: (if) I just wanted to know why the limitation
existed in the first place. (Though that principle might just be there to
make it easier for compiler writers)
regards
NPV