This is the mail archive of the gsl-discuss@sourceware.org mailing list for the GSL project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: const gsl_vector *


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]