www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Help translating C/C++ snippet to D

reply "Dustin" <dustin.sneeden gmail.com> writes:
Hello,
I'm trying to follow along with a C++ tutorial and translate it 
to D but I don't know C/C++ well enough to understand this 
#Define statement:

#define ARRAY_COUNT( array ) (sizeof( array ) / (sizeof( array[0] 
) * (sizeof( array ) != sizeof(void*) || sizeof( array[0] ) <= 
sizeof(void*))))

Can anyone help me understand this and translate it to a D 
function? Thanks for your time.
Jul 02 2012
next sibling parent reply "nazriel" <nazriel6969 gmail.com> writes:
On Tuesday, 3 July 2012 at 02:34:04 UTC, Dustin wrote:
 Hello,
 I'm trying to follow along with a C++ tutorial and translate it 
 to D but I don't know C/C++ well enough to understand this 
 #Define statement:

 #define ARRAY_COUNT( array ) (sizeof( array ) / (sizeof( 
 array[0] ) * (sizeof( array ) != sizeof(void*) || sizeof( 
 array[0] ) <= sizeof(void*))))

 Can anyone help me understand this and translate it to a D 
 function? Thanks for your time.
http://dpaste.dzfl.pl/481e26b6 It's quite simple. Macros in C++, should be replaced with their successor - templates. In case of D, we haven't got macros per se, so we need to use template. In example above I just used template'd function.
Jul 02 2012
parent "Dustin" <dustin.sneeden gmail.com> writes:
On Tuesday, 3 July 2012 at 04:09:22 UTC, nazriel wrote:
 On Tuesday, 3 July 2012 at 02:34:04 UTC, Dustin wrote:
 Hello,
 I'm trying to follow along with a C++ tutorial and translate 
 it to D but I don't know C/C++ well enough to understand this 
 #Define statement:

 #define ARRAY_COUNT( array ) (sizeof( array ) / (sizeof( 
 array[0] ) * (sizeof( array ) != sizeof(void*) || sizeof( 
 array[0] ) <= sizeof(void*))))

 Can anyone help me understand this and translate it to a D 
 function? Thanks for your time.
http://dpaste.dzfl.pl/481e26b6 It's quite simple. Macros in C++, should be replaced with their successor - templates. In case of D, we haven't got macros per se, so we need to use template. In example above I just used template'd function.
Thanks so much!!
Jul 03 2012
prev sibling next sibling parent "Tongzhou Li" <zhangsongcui hotmail.com> writes:
On Tuesday, 3 July 2012 at 02:34:04 UTC, Dustin wrote:
 Hello,
 I'm trying to follow along with a C++ tutorial and translate it 
 to D but I don't know C/C++ well enough to understand this 
 #Define statement:

 #define ARRAY_COUNT( array ) (sizeof( array ) / (sizeof( 
 array[0] ) * (sizeof( array ) != sizeof(void*) || sizeof( 
 array[0] ) <= sizeof(void*))))

 Can anyone help me understand this and translate it to a D 
 function? Thanks for your time.
Just use this:
 int[123] i;
 writeln(i.length);
 int* p;
 writeln(p.length); // Compile error
Bye!
Jul 03 2012
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On 7/3/2012 11:34 AM, Dustin wrote:
 Hello,
 I'm trying to follow along with a C++ tutorial and translate it to D but
 I don't know C/C++ well enough to understand this #Define statement:

 #define ARRAY_COUNT( array ) (sizeof( array ) / (sizeof( array[0] ) *
 (sizeof( array ) != sizeof(void*) || sizeof( array[0] ) <= sizeof(void*))))

 Can anyone help me understand this and translate it to a D function?
 Thanks for your time.
There is no reason to translate this to D. C++ doesn't keep track of array lengths out of the box, so programmers either have to do it themselves or implement some hackery like this macro to figure it out. In D, none of that is necessary. myArray.length Same thing.
Jul 03 2012
parent "Dustin" <dustin.sneeden gmail.com> writes:
On Tuesday, 3 July 2012 at 10:23:06 UTC, Mike Parker wrote:
 On 7/3/2012 11:34 AM, Dustin wrote:
 Hello,
 I'm trying to follow along with a C++ tutorial and translate 
 it to D but
 I don't know C/C++ well enough to understand this #Define 
 statement:

 #define ARRAY_COUNT( array ) (sizeof( array ) / (sizeof( 
 array[0] ) *
 (sizeof( array ) != sizeof(void*) || sizeof( array[0] ) <= 
 sizeof(void*))))

 Can anyone help me understand this and translate it to a D 
 function?
 Thanks for your time.
There is no reason to translate this to D. C++ doesn't keep track of array lengths out of the box, so programmers either have to do it themselves or implement some hackery like this macro to figure it out. In D, none of that is necessary. myArray.length Same thing.
Ok cool that's why I like D much more than C++, it gets rid of a lot of ambiguity. Thanks for the info.
Jul 03 2012