www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template constraint: T is a value type

reply "Gareth Foster" <shenlok gmail.com> writes:
Hi there,

I'm looking to write a function template that operates only on 
value types. Basically I'm looking for an equivalent of

   where T: struct



I was thinking that maybe using

   template Foo(T) if (is(T : struct) or is(T: union)) { ... }

or perhaps

   template Foo(T) if (!is(T : class)) { ... }

would be correct, but I fear the first may be too limiting (would 
simple data types such as int, char etc be allowed here? I'd 
guess not.) and the second may be too lax.

Any advice? Am I even going in remotely the right direction? 
Thanks.
Oct 29 2014
next sibling parent Rikki Cattermole <alphaglosined gmail.com> writes:
On 29/10/2014 11:01 p.m., Gareth Foster wrote:
 Hi there,

 I'm looking to write a function template that operates only on value
 types. Basically I'm looking for an equivalent of

    where T: struct



 I was thinking that maybe using

    template Foo(T) if (is(T : struct) or is(T: union)) { ... }

 or perhaps

    template Foo(T) if (!is(T : class)) { ... }

 would be correct, but I fear the first may be too limiting (would simple
 data types such as int, char etc be allowed here? I'd guess not.) and
 the second may be too lax.

 Any advice? Am I even going in remotely the right direction? Thanks.
Code: struct Foo {} union Bar {} class Fuz {} pragma(msg, __traits(isPOD, Foo)); pragma(msg, __traits(isPOD, Bar)); pragma(msg, __traits(isPOD, Fuz)); pragma(msg, __traits(isPOD, int)); pragma(msg, is(Foo == struct)); pragma(msg, is(Bar == struct)); pragma(msg, is(Fuz == struct)); pragma(msg, is(int == struct)); pragma(msg, is(Foo == union)); pragma(msg, is(Bar == union)); pragma(msg, is(Fuz == union)); pragma(msg, is(int == union)); Output: true true true true false false false false true false false Dpaste link: http://dpaste.dzfl.pl/921fa5156290 So to answer your question, first was pretty much correct.
Oct 29 2014
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 29 October 2014 at 10:01:18 UTC, Gareth Foster 
wrote:
 Hi there,

 I'm looking to write a function template that operates only on 
 value types. Basically I'm looking for an equivalent of

   where T: struct



 I was thinking that maybe using

   template Foo(T) if (is(T : struct) or is(T: union)) { ... }

 or perhaps

   template Foo(T) if (!is(T : class)) { ... }

 would be correct, but I fear the first may be too limiting 
 (would simple data types such as int, char etc be allowed here? 
 I'd guess not.) and the second may be too lax.

 Any advice? Am I even going in remotely the right direction? 
 Thanks.
std.traits.hasIndirections any use for you?
Oct 29 2014
parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Wednesday, 29 October 2014 at 11:49:59 UTC, John Colvin wrote:
 On Wednesday, 29 October 2014 at 10:01:18 UTC, Gareth Foster 
 wrote:
 Hi there,

 I'm looking to write a function template that operates only on 
 value types. Basically I'm looking for an equivalent of

  where T: struct



 I was thinking that maybe using

  template Foo(T) if (is(T : struct) or is(T: union)) { ... }

 or perhaps

  template Foo(T) if (!is(T : class)) { ... }

 would be correct, but I fear the first may be too limiting 
 (would simple data types such as int, char etc be allowed 
 here? I'd guess not.) and the second may be too lax.

 Any advice? Am I even going in remotely the right direction? 
 Thanks.
std.traits.hasIndirections any use for you?
To more complete this: template Foo(T) if(!hasIndirections!T) http://dlang.org/phobos/std_traits.html#hasIndirections
Oct 30 2014