From: John D Lamb <J.D.Lamb@btinternet.com>
To: gsl-discuss@sourceware.org
Subject: Re: const gsl_vector *
Date: Wed, 20 Aug 2014 06:37:00 -0000 [thread overview]
Message-ID: <53F44225.5070209@btinternet.com> (raw)
In-Reply-To: <53F3CF92.3030209@colorado.edu>
On 19/08/14 23:28, Patrick Alken wrote:
>> One issue would be that if the code were rewritten any code that passed
>> a const gsl_vector* argument would cause a compilation failure. The
>> workaround might be to use a
>>
>> gsl_vector const* const v
>>
>> argument. But I donÂ’t know if that would cause problems, for example
>> with older compilers.
>>
> I don't have a solution to this problem, but with my gcc (4.4.7),
> changing to gsl_vector* const doesn't fix the issue. Compiling with
> -Wall -W produces no warnings and the program runs as before.
Actually my suggested solution wonÂ’t work. A function declaration like
void f( gsl_vector *const v );
is treated by the compiler as equivalent to
void f( gsl_vector * v );
and itÂ’s possible to use the second form in a declaration and the first
in a definition.
The only compiler advantage of the first form is in a function
definition: you guarantee that v (though not what it points to) wonÂ’t be
changed. In a declaration it does no more than hint to the user of the
function that the data wonÂ’t be changed.
Although the gsl_vector functions canÂ’t offer a guarantee not to change
a const gsl_vector * argument, IÂ’m not sure this is a problem. In
practice they donÂ’t; so there are no surprises for the user.
I think the root problem is this. Neither C nor C++ has a way to declare
a struct const so that the data pointed to by any pointer in the struct
is also const. There are workarounds. But I donÂ’t know of any that donÂ’t
involve copying data, which is an unnecessary overhead when the only
real requirement is to reassure a function user that nothing pointed to
by a function parameter will be changed.
--
John D Lamb
next prev parent reply other threads:[~2014-08-20 6:37 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-08-13 0:03 Gerard Jungman
2014-08-19 9:19 ` John D Lamb
2014-08-19 22:28 ` Patrick Alken
2014-08-20 6:37 ` John D Lamb [this message]
2014-08-19 22:33 ` Patrick Alken
2014-08-27 0:08 ` Gerard Jungman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=53F44225.5070209@btinternet.com \
--to=j.d.lamb@btinternet.com \
--cc=gsl-discuss@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).