__has_side_effects

Stefan Koch uplink.coder at googlemail.com
Fri Mar 30 20:35:54 UTC 2018


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.


More information about the Digitalmars-d mailing list