[petsc-dev] Problem with PETSC_NULL and variadic functions

Jed Brown jedbrown at mcs.anl.gov
Wed Nov 9 15:06:57 CST 2011


On Wed, Nov 9, 2011 at 14:51, Barry Smith <bsmith at mcs.anl.gov> wrote:

>  In C we could do #define PETSC_NULL          ((void*) 0) and every thing
> would be fine
>

yes


>
>    In C++ this won't work and there is no alternative except a standard
> that is 2 months old (but for those programming in C++ this is not really a
> problem because there is really no need for varargs in proper C++ code).
>

unless they want to call our variadic functions


>
>  Questions:
>     1) Why won't it work in C++? Note that PETSC_NULL truly is suppose to
> always be used as a null pointer and should never be used as 0; if int 0 is
> intended then int 0 should be used.
>

C++ needs an explicit cast to convert from void*. So

some_type *x = (void*)0;

is a compilation error. It's too much to ask for an explicit cast any time
PETSC_NULL is passed.


>
>     2) Can we at least fix it for C by using #define PETSC_NULL
>  ((void*) 0)  in C and using 0 in C++. After all nobody really uses PETSc
> from C++ :-)
>

This will work. They should be able to pass nullptr in C++11, NULL with GCC
and some other compilers, or (void*)0 only in variadic slots (because those
types aren't checked by the compiler).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20111109/607ccbc1/attachment.html>


More information about the petsc-dev mailing list