Passing NULL as a const reference argument

Started by
13 comments, last by BitMaster 14 years, 5 months ago
There a string, an empty string, and a no-string-at-all. I'd expect the latter from passing NULL. Represent that!
Advertisement
Quote:Original post by BitMaster
Zahlman asked what behaviour should be expected from passing a null pointer. This was not asking what the standard says (it is clear what it says), but what can be expected to be reasonable behaviour if it were allowed.

No Zahlman asked "What do you expect to happen when that pointer is NULL?"

Not what you expect in an imaginary world to happen.
To answer his question you should have replied "Well is is not defined what will happen as it is not allowed. I expect it will start construction yet fail when again it uses undefined behaviour in a C function strlen which requires a valid pointer. So I expect nothing."
Quote:Original post by dmail
Quote:Original post by BitMaster
Zahlman asked what behaviour should be expected from passing a null pointer. This was not asking what the standard says (it is clear what it says), but what can be expected to be reasonable behaviour if it were allowed.

No Zahlman asked "What do you expect to happen when that pointer is NULL?"

Zahlman asked that to hirez. Furthermore, BitMaster said:
Quote:Original post by BitMaster
Personally I would expect it as reasonable to construct an empty string object out of it.

I.e., it's an opinion.
If the code crashes (or throws an exception - GCC), I get what I expected - a non-string :)
Quote:Original post by dmail
Quote:Original post by BitMaster
[...]

No Zahlman asked "What do you expect to happen when that pointer is NULL?"

Not what you expect in an imaginary world to happen.
To answer his question you should have replied "Well is is not defined what will happen as it is not allowed. I expect it will start construction yet fail when again it uses undefined behaviour in a C function strlen which requires a valid pointer. So I expect nothing."


Exactly. He asked what someone expected to happen. And I told him what I would expect to happen. The word 'would' (nevermind the 'expect it as reasonable') already implies sufficiently that my scenario diverges from the one mandated by the standard.
Incidentally, Qt's QString allows constructing from a null pointer and produces the intuitive result (an empty string), although they unfortunately do not comment about it either way in the documentation.

This topic is closed to new replies.

Advertisement