Template constraints for reference/value types?

Jon Degenhardt via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 6 21:22:45 PDT 2016


On Wednesday, 7 September 2016 at 00:40:27 UTC, Jonathan M Davis 
wrote:
> On Tuesday, September 06, 2016 21:16:05 Jon Degenhardt via 
> Digitalmars-d-learn wrote:
>> On Tuesday, 6 September 2016 at 21:00:53 UTC, Lodovico Giaretta
>>
>> wrote:
>> > On Tuesday, 6 September 2016 at 20:46:54 UTC, Jon Degenhardt
>> >
>> > wrote:
>> >> Is there a way to constrain template arguments to reference 
>> >> or value types? I'd like to do something like:
>> >>
>> >> T foo(T)(T x)
>> >>
>> >>     if (isReferenceType!T)
>> >>
>> >> { ... }
>> >>
>> >> --Jon
>> >
>> > You can use `if(is(T : class) || is(T : interface))`.
>> >
>> > If you also need other types, std.traits contains a bunch of 
>> > useful templates: isArray, isAssociativeArray, isPointer, ...
>>
>> Thanks. This looks like a practical approach.
>
> It'll get you most of the way there, but I don't think that 
> it's actually possible to test for reference types in the 
> general case
>
> [snip]
>
> - Jonathan M Davis

Thanks, very helpful. I've concluded that what I wanted to do 
isn't worth pursuing at the moment (see the thread on associative 
arrays in the General forum). However, your description is 
helpful to understand the details involved.


More information about the Digitalmars-d-learn mailing list