Postmortem: Template unittests are bad & you shouldn't catch Error
Jacob Carlborg
doob at me.com
Fri Oct 23 09:05:41 UTC 2020
On Thursday, 22 October 2020 at 16:18:14 UTC, H. S. Teoh wrote:
> template MyTemplate(T)
> {
> auto myFunc(...) { ... }
>
> /// This is instantiated once per template instantiation
> unittest {
> pragma(msg, T.stringof);
> }
>
> /// This is instantiated only once, ever
> /// (The use of 'static' is hypothetical syntax, it can
> /// be anything else that marks the unittest as
> /// single-instance)
> static unittest {
> //pragma(msg, T.stringof); // Error: cannot reference T here
>
> // Have to explicitly instantiate the template
> // with the arguments you want to test for
> alias U = MyTemplate!int;
> }
> }
Might be a bit confusing when the unit test is inside a templated
class. Because the unit test function is already static, relative
to the class.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list