Re: Get gchar* from Glib::ustring



Am Donnerstag, den 04.06.2009, 10:05 +0200 schrieb Mark Roberts:
> Dear Fabrício,
> 
> > > > And note that because the argument is untyped (it is an elipsis
> > > > argument) you cannot use the normal C++ 0 as a synonym for NULL.
> 
> > > An explicit cast of 0 to pointer type is in fact the only safe way.
> 
> > Well, I'm somewhat confused. Should I use "static_cast<void*>(0)"?
> > Each item is not char (16 bit)?

To summarize: With variadic arguments, all type conversions need to be
explicit, because the compiler doesn't know the expected argument type.

In this particular case, I'd use static_cast<char*>(0) because the
argument is expected to be a pointer to char.  As it happens, the C
standard says that

a) in a varargs context, any pointer argument is promoted to the generic
void* pointer representation, and

b) void* and char* are defined to have the same representation anyway.

Either rule implies that static_cast<void*> works, too.

Arguably, the most elegant way to pass a varargs sentinel pointer is
probably:

  foo = g_build_filename(bar, fuzz, gpointer());

The catch is that you can't get away without the typedef.

> Your arguments to g_build_filename() are pointers to characters. On a
> 32bit system a pointer is 32bit, on a 64bit system it is 64bit. "NULL" is 
> a pointer to anything (therefore also 64bit on a 64bit system), while "0" 
> is an integer, which might be 32bit on a 64bit system. Or it might not be.
> 
> If you know what "static_cast<void*>(0)" means, then use it and feel
> clever. If you don't, use "((void*)0)" and feel practical.

Dunno about clever, but if you know what ((void*)0) means, you also know
what static_cast<void*>(0) means.  At least if you are a C++ programmer.

But as we are now aiming for cleverness, how about:

  foo = Glib::build_filename(bar, fuzz);

?

--Daniel




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