Interfacing to C: const or immutable?

Michel Fortin michel.fortin at michelf.com
Thu Aug 26 18:13:42 PDT 2010


On 2010-08-26 18:06:24 -0400, "Mathias Laurenz Baumann" 
<anonym001 at supradigital.org> said:

> http://www.digitalmars.com/d/2.0/interfaceToC.html still says there is 
> no  const/immutable in D:
> 
>     There are no const or volatile type modifiers in D. To declare a C  
> function that uses those type modifiers, just drop those keywords from 
> the  declaration.
> 
> but that is obviously out-dated. What should I use now, when 
> interfacing  with C?

Most of the time, you'll want to use const when C does. But take note 
that const in D is transitive, so if you have a const pointer to 
mutable data in C you can't express that in D. I'd say just drop const 
for those cases.

As for immutable, there's no such thing in C. If a function is 
documented as requiring a pointer to data that will never change, then 
it might make some sense to make the argument immutable instead of 
const. Also, if a C function returns a pointer to data that you 
positively know will never change, you could make it immutable too. But 
be careful with immutable: using it carelessly might cause the compiler 
to make the wrong assumptions and cause bugs. When in doubt, use const.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list