Skip to content

const-correct public C API #844

@castedo

Description

@castedo

Following up on discussion with @molpopgen and @jeromekelleher regarding possibility of making tskit 1.0 const-correct (const-correct in the API being use of "const" in pointer parameter types declaration when API functions do not change the data pointed to).

I can't think of any downside for the callers of the API to have those parameter types get const'ed and I don't believe it will break any users (unless they are doing something unusual like keeping pointers to functions and assigning API functions to those function pointers).

In the public API, this is convenient to API users who use const often (e.g. @molpopgen and many C++ programmers). It is less annoying too. They don't have to cast away const-ness when calling the API.

However, for tskit implementors this can become a pain. This page has a nice description of the trade-off and uses the term
"const poisoning":
https://wiki.sei.cmu.edu/confluence/display/c/DCL00-C.+Const-qualify+immutable+objects

I can suggest initially only focusing on making the public API const-correct and not worrying about the broader use of const in the implementation of tskit. If there is a const issue introduced by this const type change, one can just blindly do something like this:

int public_api_func(double *values)
{
   ... code not "speaking in const" ...
}

goes to

int public_api_func(double const *_values)
{
    double *values = (double*)_values;
   ... same code not "speaking in const" ...
}

And if there is no const issue, then no need to do this.

In a sense, this is putting the burden of casting away const-ness on the implementors of tskit rather than users who call it. Which seems to be the right place to put that burden.

Metadata

Metadata

Assignees

Labels

C APIIssue is about the C APIenhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions