Type tuple of all constructors?

Jakob Ovrum jakobovrum+ng at gmail.com
Thu Sep 22 09:33:57 PDT 2011


On 2011/09/23 1:11, Benjamin Thaut wrote:
> Is there a way to get a type tuple of all aviable constructors in a class?
>
> class Test {
> public:
> this(Foo foo){
> m_foo = foo;
> }
>
> this(bool flop){
> }
>
> this(int x, int y){
> }
> }
>
> For this class I would expect something like ((Foo),(bool),(int,int))

$ cat test.d
struct Foo {}

class Test {
   public:
     this(Foo foo){
     }

     this(bool flop){
     }

     this(int x, int y){
     }
}

import std.traits;
import std.stdio;

void main()
{
	foreach(ctor; __traits(getOverloads, Test, "__ctor"))
	{
		alias ParameterTypeTuple!(typeof(ctor)) args;
		writeln(args.stringof);
	}
}

$ ./test
(Foo)
(bool)
(int, int)


More information about the Digitalmars-d mailing list