digitalmars.D.learn - Conditional nothrow/safe/nogc/etc?
- Matt Elkins (11/11) Jan 29 2016 Is there any way to specify that a generic function is
- Rikki Cattermole (5/16) Jan 29 2016 templated functions have attribute inference. Meaning if it can be
- Matt Elkins (3/9) Jan 29 2016 Convenient! Thanks!
- H. S. Teoh via Digitalmars-d-learn (16/26) Jan 29 2016 A common idiom that we use is to write an attributed unittest to verify
- Matt Elkins (2/16) Jan 29 2016 Seems sound. Thanks!
Is there any way to specify that a generic function is conditionally nothrow (or other function decorators), based on whether the template instantiation is nothrow? I'm looking for something akin to C++'s noexcept(noexcept(<expr>)), e.g.: template <class T> void foo() noexcept(noexcept(T())) {} I don't see anything about it on the functions grammar page (https://dlang.org/spec/function.html). I do see something to do with nothrow_ on the std.traits page, but I'm not sure how to use it to achieve this or whether that is the right approach. My actual use case is a non-generic method opAssign inside of a generic struct.
Jan 29 2016
On 30/01/16 6:17 PM, Matt Elkins wrote:Is there any way to specify that a generic function is conditionally nothrow (or other function decorators), based on whether the template instantiation is nothrow? I'm looking for something akin to C++'s noexcept(noexcept(<expr>)), e.g.: template <class T> void foo() noexcept(noexcept(T())) {} I don't see anything about it on the functions grammar page (https://dlang.org/spec/function.html). I do see something to do with nothrow_ on the std.traits page, but I'm not sure how to use it to achieve this or whether that is the right approach. My actual use case is a non-generic method opAssign inside of a generic struct.templated functions have attribute inference. Meaning if it can be nothrow it will be. Regarding your real use case, again struct if templated so it should be inferred.
Jan 29 2016
On Saturday, 30 January 2016 at 05:25:49 UTC, Rikki Cattermole wrote:On 30/01/16 6:17 PM, Matt Elkins wrote:Convenient! Thanks![...]templated functions have attribute inference. Meaning if it can be nothrow it will be. Regarding your real use case, again struct if templated so it should be inferred.
Jan 29 2016
On Sat, Jan 30, 2016 at 05:37:07AM +0000, Matt Elkins via Digitalmars-d-learn wrote:On Saturday, 30 January 2016 at 05:25:49 UTC, Rikki Cattermole wrote:A common idiom that we use is to write an attributed unittest to verify that the function itself is safe/etc.. This way, if instantiated with safe/etc. types, the template will also be safe/etc., but if instantiated with an unsafe type, it will correspondingly be unsafe (instead of failing to compile if you wrote safe on the template). The unittest ensures that you do not accidentally introduce un- safe code into the template and cause *all* instantiations to become un- safe. auto mySafeCode(T)(T t) { ... } safe unittest { auto x = mySafeCode(safeValue); } T -- Why are you blatanly misspelling "blatant"? -- Branden RobinsonOn 30/01/16 6:17 PM, Matt Elkins wrote:Convenient! Thanks![...]templated functions have attribute inference. Meaning if it can be nothrow it will be. Regarding your real use case, again struct if templated so it should be inferred.
Jan 29 2016
On Saturday, 30 January 2016 at 05:57:34 UTC, H. S. Teoh wrote:A common idiom that we use is to write an attributed unittest to verify that the function itself is safe/etc.. This way, if instantiated with safe/etc. types, the template will also be safe/etc., but if instantiated with an unsafe type, it will correspondingly be unsafe (instead of failing to compile if you wrote safe on the template). The unittest ensures that you do not accidentally introduce un- safe code into the template and cause *all* instantiations to become un- safe. auto mySafeCode(T)(T t) { ... } safe unittest { auto x = mySafeCode(safeValue); } TSeems sound. Thanks!
Jan 29 2016