www.digitalmars.com         C & C++   DMDScript  

D - initialization sematics and type traits

reply Sean Kelly <sean ffwd.cx> writes:
Say I have a template and I want to create a copy of a passed value:

template X(T) {
     T val;
     void set( T v ) {
         val = new T( v );
     }
}

This works fine for class types but not primitive types.  While I can 
use regular assignment for primitive types, I need a way to determine 
whether T is a primitive type.  Does D have any feature built-in where I 
can accomplish this?  I'd prefer to avoid having to create the complex 
traits template classes needed in C++.


Sean
Feb 10 2004
next sibling parent reply Patrick Down <pat codemoon.com> writes:
Sean Kelly <sean ffwd.cx> wrote in news:c0c7st$i3i$1 digitaldaemon.com:

 Say I have a template and I want to create a copy of a passed value:
 
You need to specialize the template. template X(T) { T val; void set( T v ) { val = v; } } template X(T : Object) { T val; void set( T v ) { val = new T( v ); } }
 
 This works fine for class types but not primitive types.  While I can 
 use regular assignment for primitive types, I need a way to determine 
 whether T is a primitive type.  Does D have any feature built-in where
 I can accomplish this?  I'd prefer to avoid having to create the
 complex traits template classes needed in C++.
 
 
 Sean
 
 
Feb 10 2004
parent Sean Kelly <sean ffwd.cx> writes:
Patrick Down wrote:
 
 You need to specialize the template.
Okay so I suppose I can do something like this: template gen( Ty ) { Ty copy( Ty val ) { return val; } } template gen( Ty : Object ) { Ty copy( Ty val ) { return new Ty( val ); } } But it seems that a default copy ctor isn't generated for class types, so the above trick doesn't always work. And AFAIK using the assignment operator will always result in both class variables referring to the same instance. ie. class A {} A a = new A(); A b = new A(); b = a; So in the above example, the instance of b is discarded by the assignment so both a and b refer to the same memory location. Is there any standard way to generate a copy of an arbitrary class? Sean
Feb 10 2004
prev sibling parent reply Sam McCall <tunah.d tunah.net> writes:
 This works fine for class types but not primitive types.
Or more generally, reference types but not value types? Usually (I think) the only reference types you care about are classes, you can do if((Object)val) { // reference semantics } else { // value semantics } I don't know about function pointer/delegates as types, and whether they're allowed as templates, that probably wouldn't work. A more robust solution would be good anyway. Sam
Feb 11 2004
parent Sean Kelly <sean ffwd.cx> writes:
Sam McCall wrote:
 I don't know about function pointer/delegates as types, and whether 
 they're allowed as templates, that probably wouldn't work.
 A more robust solution would be good anyway.
Ideally, containers should be able to hold anything that can be copied, regardless of the semantics. The template I posted in response to Patrick is the best I've done so far. Sean
Feb 11 2004