Can we deprecate "D-style Variadic Functions"

Freddy via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 25 18:04:03 PDT 2015


On Thursday, 26 March 2015 at 00:11:05 UTC, Dicebot wrote:
> On Wednesday, 25 March 2015 at 22:12:04 UTC, Freddy wrote:
>> "D-style Variadic Functions" found 
>> here:http://dlang.org/function.html seem entirely out classed 
>> by Variadic Function Templates. Can we deprecate them?
>
> Those are two different concepts with different trade-offs. 
> Using variadic templates adds template bloat. Using D-style 
> variadics requires RTTI (and small overhead for it). It is up 
> to library/application writer to decide what is best in his 
> case.

My ploblem is that Variadic Function shouldn't be builtin the 
language. They are rarely need and can be abstracted into a 
library. Something like this:
```
import core.stdc.stdlib: alloca;
import std.stdio;
template VariadicFunction(alias Imp){
	auto VariadicFunction(T...)(T args){
		enum size=T.length * TypeInfo.sizeof;
		auto rtti=cast(TypeInfo[])(alloca(size)[0..size]);
		foreach(i,type;T){
			rtti[i]=typeid(type);
		}
		//auto data=&args; bug? doesn't work
		void* data;
		{
			size_t datasize;//T.sizeof doesn't work
			foreach(type;T){
				datasize+=type.sizeof;
			}
			data=alloca(datasize);
			size_t inc;
			foreach(v;args){
				*cast(typeof(v)*)(data+inc)=v;
				inc+=v.sizeof;
			}
		}
		Imp(data,rtti);
	}
}

private void rtVariadicImp(void* vars,scope const TypeInfo[] 
rtinfo){
	writeln(*cast(int*)vars);
	writeln(rtinfo);
}

alias rtVariadic=VariadicFunction!(rtVariadicImp);

void main(){
	rtVariadic(1,'a');
}
```


More information about the Digitalmars-d mailing list