Function accepting variadic arguments
Daniel Kozak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jan 20 05:04:23 PST 2016
V Wed, 20 Jan 2016 12:38:20 +0000
pineapple via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
napsáno:
> I'd like to make a constructor which takes a variable list of
> arguments, of a short list of possible types, with different
> handling depending on the type. I haven't been able to find any
> documentation or examples that did quite what I'm trying to. Help?
>
> A fun pseudocode example of what I'm trying to do:
>
> this(...){
> foreach(argument){
> if(argument is a number){
> do stuff with it;
> }else if(argument is of type A){
> do other stuff;
> }else if(argument is of type B){
> yet more stuff;
> }
> }
> }
>
import std.stdio;
struct S
{
this(Args...)(Args args)
{
foreach(Type, Value;args)
{
writefln("Type: %s value: %s", Args[Type].stringof, Value);
}
}
}
void main()
{
auto s = S(1, "string");
}
More information about the Digitalmars-d-learn
mailing list