Thread overview
Help translating C/C++ snippet to D
Jul 03, 2012
Dustin
Jul 03, 2012
nazriel
Jul 03, 2012
Dustin
Jul 03, 2012
Tongzhou Li
Jul 03, 2012
Mike Parker
Jul 03, 2012
Dustin
July 03, 2012
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.
July 03, 2012
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.
July 03, 2012
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!
July 03, 2012
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!!
July 03, 2012
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.



July 03, 2012
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.