__has_side_effects

Shachar Shemesh shachar at weka.io
Sat Mar 31 19:18:24 UTC 2018


On 30/03/18 23:35, Stefan Koch wrote:
> On Friday, 30 March 2018 at 20:28:27 UTC, Andrei Alexandrescu wrote:
>> https://www.reddit.com/r/programming/comments/88aqsf/the_joy_of_max/
>>
>> Discussion aside, I notice with pleasant surprise gcc has an 
>> introspection primitive we didn't think of: __is_constant that (I 
>> assume) yields true if the given expression has no side effects.
>>
>> In D the primitive would be called e.g. __has_side_effects to avoid 
>> confusion with the "const" qualifier.
>>
>> I wonder how difficult it would be to define __has_side_effects (it 
>> would apply to an alias) and how it can be put to good use.
>>
>>
>> Andrei
> 
> It's actually quite easy.
> make a function literal annotated with pure and call your function, if 
> that returns the function had no side effects.
> if something is constant you can determine by using at in ctfe context, 
> if that function call does not compile then it is not a constant.

That would work in C++. It does not work with D. D is willing to call 
functions that have side effects "pure".

struct S {
   int a;

   void func(int b) pure {
     // For some strange reason, this is not considered a pure violation.
     a+=b;
   }
}


More information about the Digitalmars-d mailing list