[petsc-dev] marking objects as const in PETSc methods

Jed Brown jed at 59A2.org
Thu Dec 2 14:23:39 CST 2010


On Wed, Dec 1, 2010 at 20:27, Barry Smith <bsmith at mcs.anl.gov> wrote:

> A more difficult example is that the Jacobian matrix argument to SNES is
> const (in some sense) but it is not to TS (since TS "adjusts" it before
> passing to SNES).
>

This does not happen with TSComputeIJacobian, the user produces exactly what
SNES needs.

 Note that "const" here may not have the strict C meaning of not changing
> anything in the object. For example VecNorm() is const on the vector
> elements but caches the computed norm so does change the data inside the
> object.
>

This is easy to handle by casting internally.


The trouble is, of course, that once you typedef something,

typedef struct _p_Vec *Vec;

then "const Vec" just means that the pointer is const, not the struct behind
it, thus the const keyword in

PetscErrorCode VecNorm(const Vec X,NormType,PetscReal*);

has exactly zero meaning as far as the interface is concerned.  Using

#define Vec struct _p_Vec*

instead of the typedef would cause the const to apply to the struct instead
of the pointer, but this is really ugly for lots of reasons including (note
that VecType already has this problem)

  Vec X,Y;   /* Y is a struct, yuck */

So the only way, within the C type system, to do what you want would be to

typedef const struct _p_Vec *const_Vec;

and then

PetscErrorCode VecNorm(const_Vec X,NormType,PetscReal*);

This works as you would expect, but it's an unconventional C style so I
would be apprehensive about recommending it.

Jed
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20101202/dbd15e52/attachment.html>


More information about the petsc-dev mailing list