git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/2] pkt-line: fix declaration of `set_packet_header()`

From
Jeff King <peff@peff.net>
Date
May 14, 2019, 14:44 UTC
Message-ID
<20190514144439.GB28530@sigill.intra.peff.net>
In-Reply-To
<20190514144305.GA28530@sigill.intra.peff.net>
On Tue, May 14, 2019 at 10:43:06AM -0400, Jeff King wrote:
Show 32 quoted lines
> On Tue, May 14, 2019 at 02:57:01PM +0200, Johannes Schindelin wrote:
> 
> > > But the parameter treated as a constant without getting modified
> > > during the invocation of the function is an implementation detail of
> > > the function; there is no point exposing that implementation detail
> > > to its callers.  It does not even help the compilers handling the
> > > caller's compilation unit---the parameter is passed by value, so the
> > > caller knows that the callee would not modify it without "const"
> > > there.
> > >
> > > Does the language even allow flagging "const int in the definition,
> > > int in the declaration" as a warning-worthy discrepancy?
> > 
> > Apparently it does, as MS Visual C does issue a warning (and with `/Wall`,
> > it fails).
> > 
> > In any case, I don't think that it makes sense to have a function
> > declaration whose signature differs from the definition's.
> 
> I actually agree with Junio's point that in an ideal world the
> declaration should omit details that are not relevant to the caller. But
> clearly we do not live in that world, and this is a small enough point
> that we should fix it in one direction or the other.
> 
> I do have a slight preference for going the _other_ way. There is no
> need to mark the parameter as const in the definition. It is passed by
> value, so nobody except the function body cares either way. And we have
> many function bodies where value-passed parameters (or local variables!)
> are not marked as const, even though they are only assigned to once.
> 
> I don't think that annotation is telling much to any reader of the code,
> nor to a decent optimizing compiler.

To be clear, I can live with your patch as-is, and I don't think this one site overly matters. Mostly I do not want to see "let's declare pass-by-value parameters as const" picked up as a general concept, and it seems easiest to try to squash the first instance of it. :)

-Peff
Previous: Jeff KingNext: Junio C Hamano
Message 6 of 15 in “pkt-line: fix incorrect function declaration”
  1. 0/2 pkt-line: fix incorrect function declarationJohannes Schindelin via GitGitGadget, May 13, 2019
  2. 1/2 pkt-line: fix declaration of `set_packet_header()`Johannes Schindelin via GitGitGadget, May 13, 2019
  3. Junio C HamanoMay 13, 2019
  4. Johannes SchindelinMay 14, 2019
  5. Jeff KingMay 14, 2019
  6. Jeff KingMay 14, 2019
  7. Junio C HamanoMay 15, 2019
  8. Jeff KingMay 15, 2019
  9. Johannes SchindelinMay 15, 2019
  10. Junio C HamanoMay 16, 2019
  11. Jeff KingMay 16, 2019
  12. Junio C HamanoMay 16, 2019
  13. Johannes SchindelinMay 17, 2019
  14. 2/2 parse-options: adjust `parse_opt_unknown_cb()`s declared return typeJohannes Schindelin via GitGitGadget, May 13, 2019
  15. Junio C HamanoMay 13, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.