www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Checking nogc-ness of an Allocator.allocate()

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
How do I check whether an aggregate member function (call for a 
specific argument) is  nogc or not?

I want to check whether

     Allocator.allocate(1)

(for a any Allocator) is  nogc or not?

Is

https://dlang.org/phobos/std_traits.html#hasFunctionAttributes

the way to do it?
Jan 11 2018
parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Thursday, 11 January 2018 at 12:24:36 UTC, Nordlöw wrote:
 How do I check whether an aggregate member function (call for a 
 specific argument) is  nogc or not?

 I want to check whether

     Allocator.allocate(1)

 (for a any Allocator) is  nogc or not?

 Is

 https://dlang.org/phobos/std_traits.html#hasFunctionAttributes

 the way to do it?
Is this an ok implementation: enum bool isNogc(alias fun) = (isCallable!fun && (functionAttributes!fun & FunctionAttribute.nogc)); safe pure nothrow nogc unittest { static int foo(int x) nogc pure nothrow; static int goo(int x) pure nothrow; static assert(isNogc!foo); static assert(!isNogc!goo); }
Jan 11 2018
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Thursday, 11 January 2018 at 12:32:54 UTC, Per Nordlöw wrote:
 Is this an ok implementation:

 enum bool isNogc(alias fun) = (isCallable!fun &&
                                (functionAttributes!fun &
                                 FunctionAttribute.nogc));

  safe pure nothrow  nogc unittest
 {
     static int foo(int x)  nogc pure nothrow;
     static int goo(int x) pure nothrow;
     static assert(isNogc!foo);
     static assert(!isNogc!goo);
 }
Seems to be working fine. I'd go with this version for perhaps a bit more brevity and clarity: enum bool isNogc(alias fun) = hasFunctionAttributes!(fun, " nogc"); Both functionAttributes and hasFunctionAttributes already check that the argument is a callable (which function attributes should "foo" have?), so that's unnecessary. -- Simen
Jan 11 2018
parent Seb <seb wilzba.ch> writes:
On Thursday, 11 January 2018 at 13:18:47 UTC, Simen Kjærås wrote:
 On Thursday, 11 January 2018 at 12:32:54 UTC, Per Nordlöw wrote:
 Is this an ok implementation:

 enum bool isNogc(alias fun) = (isCallable!fun &&
                                (functionAttributes!fun &
                                 FunctionAttribute.nogc));

  safe pure nothrow  nogc unittest
 {
     static int foo(int x)  nogc pure nothrow;
     static int goo(int x) pure nothrow;
     static assert(isNogc!foo);
     static assert(!isNogc!goo);
 }
Seems to be working fine. I'd go with this version for perhaps a bit more brevity and clarity: enum bool isNogc(alias fun) = hasFunctionAttributes!(fun, " nogc"); Both functionAttributes and hasFunctionAttributes already check that the argument is a callable (which function attributes should "foo" have?), so that's unnecessary. -- Simen
Author of hasFunctionAttributes here. FYI: `hasFunctionAttributes` is exactly intended to be used how you do it. functionAttributes is only there for legacy reasons. And it works nicely with inference too: https://run.dlang.io/is/9OXZct
Jan 11 2018