Peter Oliphant on Wed, 5 May 2004 19:01:32 -0500 (CDT)


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

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


Hi Gavin!
 
Long time no talk to! Smiley emoticon
 
Anyway, I will throw out my weird solution (which might not work), and then you can do the same...hehe
 
How about:
 
void doFoo(const void *param)
{
   if isStringRef( param )   // this may not be possible, killing solution
   {
      // treat as param const string&
         return;
   }
   if isCharPtr( param ) // this may not be possible, killing solution
   {
      // treat as const char*
         return;
   }
  // error
}
 
My two cents... Smiley emoticon
 
  [==P==]
----- Original Message -----
Sent: Wednesday, May 05, 2004 4:46 PM
Subject: [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
_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp