digitalmars.D.learn - C const
- simendsjo (23/23) Mar 04 2011 I'm not quite sure how to wrap c's const. This page,
- Jesse Phillips (3/9) Mar 04 2011 All in all the real answer comes down to, is the data modified. Since C ...
- simendsjo (2/11) Mar 04 2011 Thanks. Does this apply to all uses of const, or just complex members?
- Jesse Phillips (2/6) Mar 04 2011 Hopefully I'm not wrong on this, but you should even be able to change t...
- Simen kjaeraas (4/16) Mar 05 2011 And 4-bit bytes? :p
- Jesse Phillips (2/9) Mar 05 2011 Right, leave me alone I keep switching which number goes here. :)
- Bekenn (8/9) Mar 05 2011 For reference:
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
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
On 04.03.2011 23:10, Jesse Phillips wrote:simendsjo Wrote:Thanks. Does this apply to all uses of const, or just complex members?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
simendsjo Wrote:On 04.03.2011 23:10, Jesse Phillips 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.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
Jesse Phillips <jessekphillips+D gmail.com> wrote:simendsjo Wrote:And 4-bit bytes? :p -- SimenOn 04.03.2011 23:10, Jesse Phillips 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.Remember that const/immutable, and other attributes/properties aren'tgoing 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 05 2011
Simen kjaeraas Wrote:Right, leave me alone I keep switching which number goes here. :)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
Mar 05 2011
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