www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template Trick

reply "matovitch" <camille.brugel laposte.net> writes:
Hello,

I got a simple vector template :

struct Vector(T, uint N)
{
       alias type T;
       T data[N];
}

And I'd like to call a function like :

void func(V, V.type default_value)(args...);

But this (of course) doesn't work. Is there a simple and nice way
to do this ? (I'm sure there is ;-))

Thanks.
Jun 12 2013
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Jun 12, 2013 at 11:26:40PM +0200, matovitch wrote:
 Hello,
 
 I got a simple vector template :
 
 struct Vector(T, uint N)
 {
       alias type T;
[...] This line should read: alias type = T; And it should work as you wanted. T -- If you look at a thing nine hundred and ninety-nine times, you are perfectly safe; if you look at it the thousandth time, you are in frightful danger of seeing it for the first time. -- G. K. Chesterton
Jun 12 2013
parent reply "matovitch" <camille.brugel laposte.net> writes:
On Wednesday, 12 June 2013 at 21:36:38 UTC, H. S. Teoh wrote:
 On Wed, Jun 12, 2013 at 11:26:40PM +0200, matovitch wrote:
 Hello,
 
 I got a simple vector template :
 
 struct Vector(T, uint N)
 {
       alias type T;
[...] This line should read: alias type = T; And it should work as you wanted. T
This was a mistake (it's quite late) : alias T type;
Jun 12 2013
parent reply "matovitch" <camille.brugel laposte.net> writes:
To be more precise the code below doesn't compile :

struct Vector(T, uint N)
{
	alias T type;
	enum dimension = N;
	T data[N];
}

void func(V, V.type def_val) (uint i, V v)
{
	if (i < v.dimension) {
		v.data[i] = def_val;
	}
}

void main()
{
	alias Vector!(int, 3) Vec3i;
	Vec3i v;
	func!(Vec3i, 42)(2, v);
}

With the error message :

test.d(8): Error: no property 'type' for type 'V'
test.d(8): Error: V.type is used as a type
Jun 12 2013
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/12/2013 02:47 PM, matovitch wrote:
 To be more precise the code below doesn't compile :

 struct Vector(T, uint N)
 {
      alias T type;
      enum dimension = N;
      T data[N];
 }

 void func(V, V.type def_val) (uint i, V v)
 {
      if (i < v.dimension) {
          v.data[i] = def_val;
      }
 }

 void main()
 {
      alias Vector!(int, 3) Vec3i;
      Vec3i v;
      func!(Vec3i, 42)(2, v);
 }

 With the error message :

 test.d(8): Error: no property 'type' for type 'V'
 test.d(8): Error: V.type is used as a type
Here is one way: void func(V, alias def_val) (uint i, V v) if (is (typeof(def_val == V.type))) { if (i < v.dimension) { v.data[i] = def_val; } } Ali
Jun 12 2013
parent reply "matovitch" <camille.brugel laposte.net> writes:
On Wednesday, 12 June 2013 at 21:52:46 UTC, Ali Çehreli wrote:
 Here is one way:

 void func(V, alias def_val) (uint i, V v)
     if (is (typeof(def_val == V.type)))
 {
     if (i < v.dimension) {
         v.data[i] = def_val;
     }
 }

 Ali
Thank you !
Jun 12 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/12/2013 02:56 PM, matovitch wrote:
 On Wednesday, 12 June 2013 at 21:52:46 UTC, Ali Çehreli wrote:
 Here is one way:

 void func(V, alias def_val) (uint i, V v)
     if (is (typeof(def_val == V.type)))
Oops. It should be: if (is (typeof(def_val) == V.type)) Hmmm. How come the other one worked as well? Because the type of (def_val == V.type) is compiled as bool and since bool is a valid type, 'is' passes.
 {
     if (i < v.dimension) {
         v.data[i] = def_val;
     }
 }

 Ali
Thank you !
Ali
Jun 12 2013
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
matovitch:

 void func(V, V.type def_val) (uint i, V v)
 {
 	if (i < v.dimension) {
 		v.data[i] = def_val;
 	}
 }
I think something like that is not yet possible, but maybe it will be possible later. Your code has also allowed me to find a new small compiler bug that I have just filed: http://d.puremagic.com/issues/show_bug.cgi?id=10346 Bye, bearophile
Jun 12 2013
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/12/2013 02:26 PM, matovitch wrote:> Hello,
 I got a simple vector template :

 struct Vector(T, uint N)
 {
        alias type T;
You later corrected that it should be alias T type; But still, prefer the new syntax over the backward C syntax: alias type = T;
        T data[N];
 }

 And I'd like to call a function like :

 void func(V, V.type default_value)(args...);
So, that function is already defined and you are trying to call it? How are you calling it? What is the error message?
 But this (of course) doesn't work. Is there a simple and nice way
 to do this ? (I'm sure there is ;-))
It is not clear to me what the purpose is. :) Ali
Jun 12 2013