www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Variadic function template

reply "ref2401" <refactor24 gmail.com> writes:
I have a number of elements of type Data!int or Data!string,

class Data(TItem) {
	TItem _item;

	this(TItem item) {
		_item = item;
	}
}

which need to be packed into container Pack. Pack!int for 
Data!int and Pack!string for Data!string respectively.

class Pack(TItem) {
	string _name
	TItem[] _items;

	this(string name, size_t numOfItems) {
		_name = name;
		_items = new TItem[numOfItems];
	}
}

I'd like to have a function taking variable number of arguments 
of type Data!TItem that packs them into single container 
Pack!TItem, similar to this:

Pack!int p1 = pack("package1", new Data!int(24), new Data!int(78),
    new Data!int(147));
Pack!int p2 = pack("package2", new Data!int(-7), new Data!int(0),
    new Data!int(18), new Data!int(99), ... , new Data!int(128));
Pack!string p3 = pack("text pack 1", new Data!string("text1"),
    new Data!string("text2"), new Data!string("text3"));

I've tried several things but none of them works:
1) pack!(TItem, P = Data!TItems...)(string name, P params) { ... 
} 	
// compilation error

2) pack(TItem)(string name, Data!TItem...) { ... }	
// compilation error

3) pack!(P...)(string name, P params) { ... }	
// compiles, but I don't know how to get the type of parameters 
passed in.
//also I need to somehow check that all elements in 'params' have 
the same type Data!TItem

Please advise how to implement the function. Thanks.
Jul 05 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/05/2013 07:55 AM, ref2401 wrote:
 I have a number of elements of type Data!int or Data!string,

 class Data(TItem) {
      TItem _item;

      this(TItem item) {
          _item = item;
      }
 }

 which need to be packed into container Pack. Pack!int for Data!int and
 Pack!string for Data!string respectively.

 class Pack(TItem) {
      string _name
      TItem[] _items;

      this(string name, size_t numOfItems) {
          _name = name;
          _items = new TItem[numOfItems];
      }
 }

 I'd like to have a function taking variable number of arguments of type
 Data!TItem that packs them into single container Pack!TItem, similar to
 this:

 Pack!int p1 = pack("package1", new Data!int(24), new Data!int(78),
     new Data!int(147));
 Pack!int p2 = pack("package2", new Data!int(-7), new Data!int(0),
     new Data!int(18), new Data!int(99), ... , new Data!int(128));
 Pack!string p3 = pack("text pack 1", new Data!string("text1"),
     new Data!string("text2"), new Data!string("text3"));

 I've tried several things but none of them works:
 1) pack!(TItem, P = Data!TItems...)(string name, P params) { ... }
 // compilation error

 2) pack(TItem)(string name, Data!TItem...) { ... }
 // compilation error

 3) pack!(P...)(string name, P params) { ... }
 // compiles, but I don't know how to get the type of parameters passed in.
 //also I need to somehow check that all elements in 'params' have the
 same type Data!TItem

 Please advise how to implement the function. Thanks.
class Data(TItem) { TItem _item; this(TItem item) { _item = item; } } class Pack(TItem) { string _name; // Added semicolon Data!TItem[] _items; // Changed to Data!TItem this(string name) { // Removed numOfItems _name = name; } // Added add(). (Instead of this method, you can take the elements as a // constructor parameter.) void add(Data!TItem[] items...) { _items ~= items; } } Pack!TItem pack(TItem)(string name, Data!TItem[] items...) { auto p = new Pack!TItem(name); p.add(items); return p; } void main() { Pack!int p1 = pack("package1", new Data!int(24)); Pack!int p2 = pack("package2", new Data!int(-7), new Data!int(0)); Pack!string p3 = pack("text pack 1", new Data!string("text1")); } Ali
Jul 05 2013