On Fri, 3 Jun 2011, Jed Brown wrote:

> PETSc uses const for all array arguments that are const. If any are
> missing, it's a bug. PETSc does not use const for typedefs because
> of the language (scope of const as it applies to typedefs). To have
> a const-correct API, we would either need to create a bunch of
> const_Vec, const_Mat, etc. typedefs (and have users know that a Mat
> would be implicitly converted to a const_Mat, but not vice-versa) or

Yeah, that's basically the best (well, least bad) solution for
pointer-type objects in C++, although the result (e.g. const_iterator
vs iterator generics) is slightly awkward to use and sometimes much
more tedious to implement.

> typedef struct _p_Vec Vec;
> 
> (no "*") and make everyone add a "*" everywhere in their code.

That's probably what I'd have done, for a C library.  One of the C
programmer complaints about C++ is that it's weird to see
pass-by-reference semantics in function calls without a * in front of
the argument indicating it.  In standard libraries even opaque types
like FILE end up designed to be passed around with a * everywhere
rather than being defined as pointer-type objects, on the theory that
being unsurprising is better than being terse.

> The last "alternative" of
> 
> #define Vec struct _p_Vec*
> 
> instead of a typedef is atrocious

Agreed.

> > Actually, what's wrong with "const" is that the compiler *can't* use it for 
> > efficiency.
> 
> An exception: statically allocated const objects (most frequently
> arrays of strings and v-tables) are placed in read-only memory which
> reduces binary size and loading time.

Hmm... and the compiler doesn't give you a whole lot of protection
from screwing that up, does it?  g++ warns about a "deprecated
conversion from string constant to char*", gcc -Wall doesn't even give
a warning before outputting a destined-to-segfault binary.

> Const is for people. The restrict qualifier, on the other hand ...

Do you happen to know if restrict is useful in any C++ compilers?

It's been a C standard for like a decade now.  On the other hand, the
last I'd heard was that it wasn't slated for any future C++ standard
(on the theory that it was too hard to define its semantics for
complicated class or member types?).  On the gripping hand everybody
seems to support a __restrict or __restrict__ keyword in C++ that must
do *something*.  On the (is there no geeky metaphor for a?) fourth
hand, one of the more serious attempts I know of to optimize a C++
numerics code is Eigen, and they do use restrict, but they only bother
to use it for a fraction of a percent of their code.
---
Roy

------------------------------------------------------------------------------
Simplify data backup and recovery for your virtual environment with vRanger.
Installation's a snap, and flexible recovery options mean your data is safe,
secure and there when you need it. Discover what all the cheering's about.
Get your free trial download today. 
http://p.sf.net/sfu/quest-dev2dev2 
_______________________________________________
Libmesh-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libmesh-users

Reply via email to