Jump to content
  • Advertisement
Sign in to follow this  
hirez

Passing NULL as a const reference argument

This topic is 3172 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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]

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!