www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Need trait of isPod.

reply Yang Bo <pop.atry gmail.com> writes:
Because I really want to know whether a type is pod.
Oct 09 2007
parent reply Aarti_pl <aarti interia.pl> writes:
Yang Bo pisze:
 Because I really want to know whether a type is pod.
Simple template should be just enough. See: http://dsource.org/projects/tango/browser/trunk/tango/core/Variant.d template isAtomicType( T ) { static if( is( T == bool ) || is( T == char ) || is( T == wchar ) || is( T == dchar ) || is( T == byte ) || is( T == short ) || is( T == int ) || is( T == long ) || is( T == ubyte ) || is( T == ushort ) || is( T == uint ) || is( T == ulong ) || is( T == float ) || is( T == double ) || is( T == real ) || is( T == ifloat ) || is( T == idouble ) || is( T == ireal ) ) const isAtomicType = true; else const isAtomicType = false; } BR Marcin Kuszczak (aarti_pl)
Oct 09 2007
parent reply Matti Niemenmaa <see_signature for.real.address> writes:
Aarti_pl wrote:
 Yang Bo pisze:
 Because I really want to know whether a type is pod.
Simple template should be just enough. See: http://dsource.org/projects/tango/browser/trunk/tango/core/Variant.d template isAtomicType( T ) { static if( is( T == bool ) || is( T == char ) || is( T == wchar ) || is( T == dchar ) || is( T == byte ) || is( T == short ) || is( T == int ) || is( T == long ) || is( T == ubyte ) || is( T == ushort ) || is( T == uint ) || is( T == ulong ) || is( T == float ) || is( T == double ) || is( T == real ) || is( T == ifloat ) || is( T == idouble ) || is( T == ireal ) ) const isAtomicType = true; else const isAtomicType = false; }
That misses structs, which are also POD. -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Oct 09 2007
next sibling parent reply Aarti_pl <aarti interia.pl> writes:
Matti Niemenmaa pisze:
 Aarti_pl wrote:
 Yang Bo pisze:
 Because I really want to know whether a type is pod.
Simple template should be just enough. See: http://dsource.org/projects/tango/browser/trunk/tango/core/Variant.d template isAtomicType( T ) { static if( is( T == bool ) || is( T == char ) || is( T == wchar ) || is( T == dchar ) || is( T == byte ) || is( T == short ) || is( T == int ) || is( T == long ) || is( T == ubyte ) || is( T == ushort ) || is( T == uint ) || is( T == ulong ) || is( T == float ) || is( T == double ) || is( T == real ) || is( T == ifloat ) || is( T == idouble ) || is( T == ireal ) ) const isAtomicType = true; else const isAtomicType = false; }
That misses structs, which are also POD.
It should be just enough to add additional condition: (...) || is( T == struct ) (...) and also probably union and enum. More: http://digitalmars.com/d/expression.html (section: is expression) BR Marcin Kuszczak (aarti_pl)
Oct 09 2007
next sibling parent reply Paolo Invernizzi <arathorn NO_SPAMfastwebnet.it> writes:
Not related... but what's the expression for testing an associative 
array? DMD 1.0 please...

Paolo

Aarti_pl wrote:
 Matti Niemenmaa pisze:
 Aarti_pl wrote:
 Yang Bo pisze:
 Because I really want to know whether a type is pod.
Simple template should be just enough. See: http://dsource.org/projects/tango/browser/trunk/tango/core/Variant.d template isAtomicType( T ) { static if( is( T == bool ) || is( T == char ) || is( T == wchar ) || is( T == dchar ) || is( T == byte ) || is( T == short ) || is( T == int ) || is( T == long ) || is( T == ubyte ) || is( T == ushort ) || is( T == uint ) || is( T == ulong ) || is( T == float ) || is( T == double ) || is( T == real ) || is( T == ifloat ) || is( T == idouble ) || is( T == ireal ) ) const isAtomicType = true; else const isAtomicType = false; }
That misses structs, which are also POD.
It should be just enough to add additional condition: (...) || is( T == struct ) (...) and also probably union and enum. More: http://digitalmars.com/d/expression.html (section: is expression) BR Marcin Kuszczak (aarti_pl)
Oct 08 2007
parent reply Marcin Kuszczak <aarti_nospam please_interia.pl> writes:
Paolo Invernizzi wrote:

 Not related... but what's the expression for testing an associative
 array? DMD 1.0 please...
 
 Paolo
I found following template in Tango core/Traits.d: private template isAssocArrayType( T ) { const bool isAssocArrayType = is( typeof(T.init.values[0] [typeof(T.init.keys[0])] == T ); } I don't know if it really works (why is there private access qualifier???). Probably better answer could give Tango people... and tests of course :-) -- Regards Marcin Kuszczak (Aarti_pl) ------------------------------------- Ask me why... I believe in Jesus - http://www.zapytajmnie.com (en/pl) Doost (port of few Boost libraries) - http://www.dsource.org/projects/doost/ -------------------------------------
Oct 09 2007
parent reply Paolo Invernizzi <arathorn NOSPAM_fastwebnet.it> writes:
This static assert is false under GDC 0.24...

template isAssocArrayType( T )
{
    const bool isAssocArrayType = is(
typeof(T.init.values[0][typeof(T.init.keys[0])]) == T );
}
static assert( isAssocArrayType!( int[char[]] ) );

Paolo


Marcin Kuszczak Wrote:

 Paolo Invernizzi wrote:
 
 Not related... but what's the expression for testing an associative
 array? DMD 1.0 please...
 
 Paolo
I found following template in Tango core/Traits.d: private template isAssocArrayType( T ) { const bool isAssocArrayType = is( typeof(T.init.values[0] [typeof(T.init.keys[0])] == T ); } I don't know if it really works (why is there private access qualifier???). Probably better answer could give Tango people... and tests of course :-) -- Regards Marcin Kuszczak (Aarti_pl) ------------------------------------- Ask me why... I believe in Jesus - http://www.zapytajmnie.com (en/pl) Doost (port of few Boost libraries) - http://www.dsource.org/projects/doost/ -------------------------------------
Oct 10 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Paolo Invernizzi wrote:
 This static assert is false under GDC 0.24...
 
 template isAssocArrayType( T )
 {
     const bool isAssocArrayType = is(
typeof(T.init.values[0][typeof(T.init.keys[0])]) == T );
 }
 static assert( isAssocArrayType!( int[char[]] ) );
The closing bracket of the first typeof() is misplaced. The correct code is --- template isAssocArrayType( T ) { const bool isAssocArrayType = is( typeof(T.init.values[0])[typeof(T.init.keys[0])] == T ); } static assert( isAssocArrayType!( int[char[]] ) ); --- Which also seems to be how it appears in Tango. Maybe Marcin typed it over from Tango with a typo? (since the file hasn't changed for 7 months according to the svn repository, a copy-paste from an older buggier version seems unlikely)
Oct 10 2007
parent Aarti_pl <aarti interia.pl> writes:
Frits van Bommel pisze:
  > Which also seems to be how it appears in Tango. Maybe Marcin typed it
 over from Tango with a typo? (since the file hasn't changed for 7 months 
 according to the svn repository, a copy-paste from an older buggier 
 version seems unlikely)
Just for clarification: That's not my code. It's taken from Tango as I pointed out in my first post. I copied it from dsource page, so during stripping line numbers I probably lost one closing bracket. BR Marcin Kuszczak
Oct 10 2007
prev sibling parent Lutger <lutger.blijdestijn gmail.com> writes:
Aarti_pl wrote:
 Matti Niemenmaa pisze:
...
 That misses structs, which are also POD.
It should be just enough to add additional condition: (...) || is( T == struct ) (...) and also probably union and enum. More: http://digitalmars.com/d/expression.html (section: is expression) BR Marcin Kuszczak (aarti_pl)
Correct me if I'm wrong, but I thought POD requires members of a struct be public and also POD. Here is a version (quick hack) that takes these criteria into account (D1.0 syntax): template isAtomicType( T ) { static if( is( T == bool ) || is( T == char ) || is( T == wchar ) || is( T == dchar ) || is( T == byte ) || is( T == short ) || is( T == int ) || is( T == long ) || is( T == ubyte ) || is( T == ushort ) || is( T == uint ) || is( T == ulong ) || is( T == float ) || is( T == double ) || is( T == real ) || is( T == ifloat ) || is( T == idouble ) || is( T == ireal ) ) const isAtomicType = true; else static if (is(T == struct)) { static if (is (typeof(T.tupleof))) // test for private members { static if (T.tupleof.length == 0) const isAtomicType = true; else const isAtomicType = areAtomicTypes!(typeof(T.tupleof)); } else const isAtomicType = false; } else const isAtomicType = false; } // helper template template areAtomicTypes(T...) { static if (T.length == 1) const areAtomicTypes = isAtomicType!(T[0]); else const areAtomicTypes = isAtomicType!(T[0]) && areAtomicTypes!(T[1..$]); }
Oct 09 2007
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 10/9/07, Matti Niemenmaa <see_signature for.real.address> wrote:
 That misses structs, which are also POD.
So are fixed-size arrays of PODs.
Oct 09 2007