www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Variadic constructor conflict

reply "andrea9940" <no mail.plz> writes:
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...
Jan 30 2013
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
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
Jan 30 2013
parent "andrea9940" <no mail.plz> writes:
Aw
Jan 30 2013