View mode: basic / threaded / horizontal-split · Log in · Help
March 04, 2011
C const
I'm not quite sure how to wrap c's const. This page, 
http://www.digitalmars.com/d/2.0/htomodule.html, says:
""
D has const as a type modifier.
	void foo(const int *p, char *const q);
becomes:
	void foo(const int* p, const char* q);
But D's const is transitive - there are no const pointers to mutable 
types in D. When encountering such in C code, simply drop the const.
""

So const on basic types should be const in D too. It also says "char 
const* q". Is "const char*" the same thing in C?

But this page, http://www.digitalmars.com/d/2.0/interfaceToC.html, says:
""
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.
""
So all const modifiers should be dropped everywhere..?

And should the const be dropped here?
struct somestruct {
  const struct otherstruct;
}
March 04, 2011
Re: C const
simendsjo Wrote:

> So all const modifiers should be dropped everywhere..?
> 
> And should the const be dropped here?
> struct somestruct {
>    const struct otherstruct;
> }

All in all the real answer comes down to, is the data modified. Since C makes no guarantees you must only declare things const if you know the library will abide by it. In the case above I think you have to drop it.

Remember that const/immutable, and other attributes/properties aren't going to change the ABI so dropping them will be safer then leaving them.
March 04, 2011
Re: C const
On 04.03.2011 23:10, Jesse Phillips wrote:
> simendsjo Wrote:
>
>> So all const modifiers should be dropped everywhere..?
>>
>> And should the const be dropped here?
>> struct somestruct {
>>     const struct otherstruct;
>> }
>
> All in all the real answer comes down to, is the data modified. Since C makes no guarantees you must only declare things const if you know the library will abide by it. In the case above I think you have to drop it.
>
> Remember that const/immutable, and other attributes/properties aren't going to change the ABI so dropping them will be safer then leaving them.

Thanks. Does this apply to all uses of const, or just complex members?
March 05, 2011
Re: C const
simendsjo Wrote:

> On 04.03.2011 23:10, Jesse Phillips wrote:
> > Remember that const/immutable, and other attributes/properties aren't going to change the ABI so dropping them will be safer then leaving them.
> 
> Thanks. Does this apply to all uses of const, or just complex members?

Hopefully I'm not wrong on this, but you should even be able to change the type as long as the size is the same. So instead of int you could use uint or byte[8]... granted the library will still interpret it as int. And of course that is assuming you are on a machine with a 32 bit int.
March 05, 2011
Re: C const
Jesse Phillips <jessekphillips+D@gmail.com> wrote:

> simendsjo Wrote:
>
>> On 04.03.2011 23:10, Jesse Phillips wrote:
>> > Remember that const/immutable, and other attributes/properties aren't  
>> going to change the ABI so dropping them will be safer then leaving  
>> them.
>>
>> Thanks. Does this apply to all uses of const, or just complex members?
>
> Hopefully I'm not wrong on this, but you should even be able to change  
> the type as long as the size is the same. So instead of int you could  
> use uint or byte[8]... granted the library will still interpret it as  
> int. And of course that is assuming you are on a machine with a 32 bit  
> int.

And 4-bit bytes? :p

-- 
Simen
March 05, 2011
Re: C const
Simen kjaeraas Wrote:

> > Hopefully I'm not wrong on this, but you should even be able to change  
> > the type as long as the size is the same. So instead of int you could  
> > use uint or byte[8]... granted the library will still interpret it as  
> > int. And of course that is assuming you are on a machine with a 32 bit  
> > int.
> 
> And 4-bit bytes? :p

Right, leave me alone I keep switching which number goes here. :)
March 05, 2011
Re: C const
On 3/4/2011 11:19 AM, simendsjo wrote:
> It also says "char const* q". Is "const char*" the same thing in C?

For reference:

In C, const T* x is the same as T const* x; both declare a mutable 
pointer to const T.  T* const x declares a const pointer to mutable T, 
for which D has no analogue.

In D, const T* x declares a const pointer to const T, which differs from 
the C syntax.  In C, that would be const T* const x (or T const * const 
x).  Thank God for D.
Top | Discussion index | About this forum | D home