Passing NULL as a const reference argument

Started by
13 comments, last by BitMaster 14 years, 5 months ago
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)?
Advertisement
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]
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]
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: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]
"You insulted me!" I did not say that in the private message Tom Sloper!
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
Personally I would expect it as reasonable to construct an empty string object out of it.
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.

Stephen M. Webb
Professional Free Software Developer

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.

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.

This topic is closed to new replies.

Advertisement