UDA example

Craig Dillabaugh cdillaba at cg.scs.carleton.ca
Mon Aug 19 12:06:14 PDT 2013


On Monday, 19 August 2013 at 18:28:24 UTC, Dicebot wrote:
> On Monday, 19 August 2013 at 18:21:26 UTC, Craig Dillabaugh 
> wrote:
>> I am still curious though was is wrong using UDAs inside 
>> main().
>
> It is not exactly about UDA - key is different meaning of curly 
> brace {} syntax in function bodies and in declaration scopes.
>
> Used in declaration scope {} apply all attributes attached to 
> it to every declaration inside:
>
> const
> {
>     int a;
>     double b;
> }
>
> static assert (is(typeof(a) == const));
>
> This also applies for UDA's.
>
> In function body it creates new variable scope - variables 
> declared inside get destroyed upon leaving the scope and are 
> not available outside.
>
> void foo()
> {
>     {
>        int a;
>     }
>     // a = 42; // error
> }
>
> As this usage of {} does not expect any attribute propagation, 
> UDA's get rejected too.

Thank you, makes perfect sense.


More information about the Digitalmars-d-learn mailing list