I have a function like this:
void Test(std::wstring test1) { cout << test1.c_str(); } When I call it like this:
Test(NULL); I get runtime exceptions like "invalid handle" and the entire program crashes. I can't do a null check on parameter 'test1' because the compiler says it can't be null. I'd like to either prevent a NULL from being passed in the first place, or have a way to check for NULLs at runtime. How can I solve this problem?
NULL?std::wstring, it should fix itself when you change the callsites to usestd::wstring. If those are in user's programs, then an overload would probably be the best solution for them not to change their code, but you can at least change the code you manage to call it correctly.LPWSTRtostd::wstringchanges the interface, as you've discovered. You either have to update the client code to adhere to the new interface or you have to do something to maintain the old interface (e.g., by adding an overload that works with NULL).Test(NULL)to do? I think you may be confusing a null pointer with an empty string. They are not the same thing though some other strings classes might treat a null pointer as a special case and construct and empty string. You probably just wantTest(L"");