Gavin Doughtie on Wed, 5 May 2004 18:46:54 -0500 (CDT)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[ALACPP] const char* NULL to std::string api


So an interesting puzzle came up at work today.

Say you have an API like so:

void doFoo(const char* param)
{
	if (NULL != param) {
		// do something
	}
}

Great. Now say you want to be modern and write the api like so:

void doFoo(const std::string& param)
{
}

Now imagine that somebody calls you with a NULL const char* param. std::string tries to implicitly convert that NULL pointer to a std::string,
and, if you are lucky and your compiler isn't a leftover from the tech boom like it might be at certain entertainment companies, the string constructor
throws a logic_error.

There is no way to guard against this except by including a const char* implementation of your interface which does the appropriate checks before forwarding to the std::string 
implementation.

The best I've come up with so far is some nasty boost::preprocessor macro to write the parellel implementation for you. Does anybody have a better idea?
_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp