On Wed, Aug 17, 2011 at 9:08 PM, Barry Smith <span dir="ltr"><<a href="mailto:bsmith@mcs.anl.gov">bsmith@mcs.anl.gov</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><br>
On Aug 17, 2011, at 2:04 PM, Chetan Jhurani wrote:<br>
<br>
>> From: Jed Brown<br>
>> Sent: Wednesday, August 17, 2011 12:38 PM<br>
>> To: For users of the development version of PETSc<br>
>> Subject: Re: [petsc-dev] declaring argument as const basic type in PETSc<br>
>><br>
>> On Wed, Aug 17, 2011 at 11:34, Barry Smith <<a href="mailto:bsmith@mcs.anl.gov">bsmith@mcs.anl.gov</a>> wrote:<br>
>>> But my question remains, should we const declare function arguments?<br>
>><br>
>> No, the prototype should never use const for stack arguments (pointer to const<br>
>> is totally different and should be used anywhere that it is correct).<br>
>><br>
>> I don't know any responsible libraries that const-ify value parameters in<br>
>> prototypes. I think it most commonly arises from ignorance of, or sloppiness<br>
>> with, the actual semantics of the C language.<br>
><br>
><br>
</div>   So by the standard a compiler should never bitch if in the function definition it is declared const but in the extern prototype it is not declared const? (This is an exception to the usual rule that the extern arguments should match the definition arguments?)<br>
</blockquote><div><br></div><div>Just so I understand, this declaration and definition:</div><div><br></div><div>void foo(const int a);</div><div><br></div><div>void foo(const int a) {</div><div>}</div><div><br></div><div>
is legal, and would prevent a developer from accidentally assigning to a, but we are outlawing it. I do not</div><div>see the rationale.</div><div><br></div><div>    Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

    Thanks<br>
<font color="#888888"><br>
  Barry<br>
</font><div><div></div><div class="h5"><br>
> It is useful to have const pass-by-value parameters in function<br>
> definitions.  Not useful in the function declarations or for the<br>
> caller.<br>
><br>
> This is so that a developer does not assign to a passed-in int<br>
> unintentionally.  Almost all C and C++ compilers accept it and they<br>
> error out if one does something like the code below.  An exception<br>
> is old IRIX compilers that emit a warning on using const pass-by-value<br>
> and said it is meaningless to use them.  I disagree.<br>
><br>
> void f(const int n, int* j)<br>
> {<br>
>    *j = n;<br>
>    n = 0; // error<br>
> }<br>
><br>
> Of course it is more useful for preventing bugs in more complex<br>
> functions.<br>
><br>
> Chetan<br>
><br>
><br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>
-- Norbert Wiener<br>