Interesting GCC extensions

Lutger lutger.blijdestijn at gmail.com
Mon Sep 28 10:24:50 PDT 2009


bearophile wrote:

> Beside the known ones, like computed gotos and __builtin_expect(), GCC has
> other less known extensions, you can find some of them here:
> http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/
> 
> They are used by Linux. If D wants to be a system language, such small
> things may be useful if you want to build a kernel with D.
> 
> One of them is the Range extension, it seems GCC devs think that 3 points
> are better after all:
> 
> switch (major_idx) {
> case 0:
> return SCSI_DISK0_MAJOR;
> case 1 ... 7:
> return SCSI_DISK1_MAJOR + major_idx - 1;
> case 8 ... 15:
> return SCSI_DISK8_MAJOR + major_idx - 8;
> default:
> BUG();
> return 0;
> }
> 
> 
> Triple points can be used for initializations too:
> int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };

That's cool, but the triple point syntax was shot down by Andrei on grounds 
of it complicating the parsing iirc. D already has .., I don't remember this 
being in C++. 

> 
> The __builtin_return_address() looks interesting, but I don't understand
> where it can be useful.
> 
> 
> The Constant detection done with __builtin_constant_p(exp) is what I was
> asking for in one of my recent posts here. It seems I was "right" again.

We don't need an extension for this! Look:

template Eval(string exp)
{
    enum Eval = mixin(exp);
}

template IsConstant(string exp)
{
    enum IsConstant = __traits(compiles, Eval!exp);
}

> 
> The article also shows some of the function attributes, in truth in GCC
> there are many other of such attributes. Some of them are useful for D
> too.
> 
> The good thing of adding some of those things to D is that they can be put
> in the specs, so they don't become nonstandard extensions as in GCC, this
> avoids several troubles.
> 
> Bye,
> bearophile

I think most of the stuff that could be in the spec is covered by __traits. 
__traits is also easily extended without breaking anything else. Some things 
mentioned in the article are very implementation specific though. 




More information about the Digitalmars-d mailing list