www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C const

reply simendsjo <simen.endsjo pandavre.com> writes:
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;
}
Mar 04 2011
next sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
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.
Mar 04 2011
next sibling parent reply simendsjo <simen.endsjo pandavre.com> writes:
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?
Mar 04 2011
parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
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.
Mar 04 2011
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
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. :)
Mar 05 2011
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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  

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
Mar 05 2011
prev sibling parent Bekenn <leaveme alone.com> writes:
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.
Mar 05 2011