August 29, 2014 [Issue 8577] static assert is triggered after tuple bounds check | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=8577 hsteoh@quickfur.ath.cx changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |hsteoh@quickfur.ath.cx --- Comment #1 from hsteoh@quickfur.ath.cx --- Workarounds: 1) ----- template T(Ts...) if (Ts.length > 1) { alias Ts[1] Z; } ----- 2) ----- template T(Ts...) { static if (Ts.length > 1) alias Ts[1] Z; else static assert(0); } ----- -- | ||||
September 08, 2022 [Issue 8577] static assert is triggered after tuple bounds check | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=8577 RazvanN <razvan.nitu1305@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED CC| |razvan.nitu1305@gmail.com Resolution|--- |INVALID --- Comment #2 from RazvanN <razvan.nitu1305@gmail.com> --- Today, the code yields: onlineapp.d(2): Error: tuple index 1 exceeds 1 onlineapp.d(7): Error: template instance `onlineapp.T!int` error instantiating And I am arguing that this is the correct behavior. The static assert is generally used to test that things inside the template instantiation are correct. Template constraints are used to check that the template arguments are correct. The static assert cannot be evaluated before the template is instantiated because that will make it useless in 99% of the cases. The general case for static asserts in template declarations is to check that specific template properties are correct, therefore, you first need to have the instantiation before you actually evaluate the static assert. In conclusion: use template constraints for this scenario: template T(Ts...) if (Ts.length > 1) { alias Ts[1] Z; } Closing as invalid. -- | ||||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply