Sign in to follow this  
hirez

Passing NULL as a const reference argument

Recommended Posts

In several versions of VC++ and MinGW it is possible to pass NULL to a const reference argument, eg:
void foo(const string &s)
{
}

foo(NULL);

Why is this possible (without any warnings even)?

Share this post


Link to post
Share on other sites
Because a null reference is not passed. The compiler calls the std::string constructor with the null pointer since a const reference to an std::string is expected.

Edit: clarification because initial post suggested more than it should have.

[Edited by - BitMaster on November 11, 2009 3:56:21 AM]

Share this post


Link to post
Share on other sites
A temporary std::string is being constructed via its string(const char *) constructor (with argument NULL), and this is being passed into foo.

The following code demonstrates converting constructors:

#include <iostream>

class C {
public:
C(void *p) {
std::cout << "C::C(" << p << ")\n";
}
};

void f(const C& c) {

}

int main() {
f(0); // Output: C::C(0)
}



If you wanted to stop this from occuring in the above code, you'd mark C's constructor as explicit, then upon compiling you receive (gcc here):
error: invalid initialization of reference of type 'const C&' from expression of type 'int'

[Edited by - mattd on November 11, 2009 2:00:46 AM]

Share this post


Link to post
Share on other sites
Thanks for clearing that up. It seems though that stl crashes in strlen related code when trying to construct a string from a NULL argument. Probably just a problem with the VS2005 stl implementation.

Share this post


Link to post
Share on other sites
Quote:
Original post by hirez
Thanks for clearing that up. It seems though that stl crashes in strlen related code when trying to construct a string from a NULL argument. Probably just a problem with the VS2005 stl implementation.


Quote:
A valid std::string instance can be constructed out of a null pointer

No I think you will find std::string (which is infact std::basic_string) and strlen require the pointer to be none NULL.

Anyway using NULL to pass to a function as a parameter has flaws which will be addressed in the next revision of the language using nullptr. For example if the function was overloaded with one that took an int or different pointer types.

I see that you have commented that it works with a reference to constant instance; yet I wonder if you realise the significance of this. As pointed out in this thread a temporary is passed as the parameter and because it takes a reference to constant this is allowed. Removing either the reference or the constness would turn this into a compile time error.

[Edited by - magic_man on November 11, 2009 3:35:12 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by hirez
Thanks for clearing that up. It seems though that stl crashes in strlen related code when trying to construct a string from a NULL argument. Probably just a problem with the VS2005 stl implementation.


The constructor used here expects a const char*, that is, a pointer to the text to use for the string. What do you expect to happen when that pointer is NULL? :P

Share this post


Link to post
Share on other sites
Quote:
Original post by BitMaster
Personally I would expect it as reasonable to construct an empty string object out of it.

Yes, it's perfectly reasonable. As is reformatting your hard drive. When invoking undefined behaviour, anything is perfectly reasonable.

Share this post


Link to post
Share on other sites
Quote:
Original post by BitMaster
Personally I would expect it as reasonable to construct an empty string object out of it.

Why would you expect that when it is not valid to pass a NULL pointer.

Quote:
basic_string(const charT* s, const Allocator& a = Allocator());
Requires: s shall not be a null pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by dmail
Quote:
Original post by BitMaster
Personally I would expect it as reasonable to construct an empty string object out of it.

Why would you expect that when it is not valid to pass a NULL pointer.

Quote:
basic_string(const charT* s, const Allocator& a = Allocator());
Requires: s shall not be a null pointer.


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.

Share this post


Link to post
Share on other sites
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."

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this