Thread overview
Variadic constructor conflict
Jan 30, 2013
andrea9940
Jan 30, 2013
Simen Kjaeraas
Jan 30, 2013
andrea9940
January 30, 2013
This code compiles fine:
------------
struct Vector(T, uint SIZE)
{
	T[SIZE] vector;

	this(T value) {
		foreach (ref v; vector) v = value;
	}
}
alias Vector!(int, 3) Vec3i;
------------

but if I add a variadic constructor:

------------
struct Vector(T, uint SIZE)
{
	[...]

	this(T...)(T values) if (values.length == SIZE) {
		foreach (i, v; values) vector[i] = v;
	}
}
alias Vector!(int, 3) Vec3i;
------------

I get:
main.d(54): Error: template main.Vector!(int, 3).Vector.__ctor(T...)(T values) if (values.length == SIZE) conflicts with constructor main.Vector!(int, 3).Vector.this at main.d(46)
main.d(111): Error: template instance main.Vector!(int, 3) error instantiating


I think that should not happen because when there is a conflict call like Vec3i(3) the compiler must(?) use the specialized function...
January 30, 2013
On 2013-01-30, 17:08, andrea9940 wrote:

> This code compiles fine:
> ------------
> struct Vector(T, uint SIZE)
> {
> 	T[SIZE] vector;
>
> 	this(T value) {
> 		foreach (ref v; vector) v = value;
> 	}
> }
> alias Vector!(int, 3) Vec3i;
> ------------
>
> but if I add a variadic constructor:
>
> ------------
> struct Vector(T, uint SIZE)
> {
> 	[...]
>
> 	this(T...)(T values) if (values.length == SIZE) {
> 		foreach (i, v; values) vector[i] = v;
> 	}
> }
> alias Vector!(int, 3) Vec3i;
> ------------
>
> I get:
> main.d(54): Error: template main.Vector!(int, 3).Vector.__ctor(T...)(T values) if (values.length == SIZE) conflicts with constructor main.Vector!(int, 3).Vector.this at main.d(46)
> main.d(111): Error: template instance main.Vector!(int, 3) error instantiating
>
>
> I think that should not happen because when there is a conflict call like Vec3i(3) the compiler must(?) use the specialized function...

Known bug. For the moment, the workaround is to templatize all constructors:

struct Vector(...)
{
    this()(T value ) {
        ...

-- 
Simen
January 30, 2013
Aw