Function attribute best practices

Paul Backus snarwin at gmail.com
Mon Sep 12 16:39:14 UTC 2022


On Monday, 12 September 2022 at 16:14:42 UTC, Ali Çehreli wrote:
> Is this accurate: Because Foo is a template, it should not put 
> any attribute on member functions? Or only member functions 
> that use a member that depends on a template parameter? And 
> non-members that are templates?

Yes. Except for `@trusted`, explicit attributes on template code 
are a smell.

> Is putting function attributes on unittest blocks for catching 
> such issues?
>
> @nogc nothrow pure @safe
> unittest
> {
>     // ...
> }
>
> No, it isn't because unless my unittest code is impure, I can't 
> catch my incorrect 'pure' etc. on my member functions.

To test that a particular piece of code *isn't* pure, you can use 
the following idiom:

     static assert(!__traits(compiles, () pure {
         // code to test goes here
     });


More information about the Digitalmars-d-learn mailing list