Can we deprecate "D-style Variadic Functions"
deadalnix via Digitalmars-d
digitalmars-d at puremagic.com
Fri Mar 27 12:59:11 PDT 2015
On Friday, 27 March 2015 at 14:24:47 UTC, Shammah Chancellor
wrote:
> On 2015-03-26 01:04:03 +0000, Freddy said:
>
>> 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');
>> }
>> ```
>
> I disagree, and your example does not get rid of the template
> bloat. That does in fact instantiate a template for every set
> of argument types.
>
> -Shammah
To be fair, this is most likely going to be inlined an ditched
away with any decent optimizer.
More information about the Digitalmars-d
mailing list