On Mon, 05 Jan 2009 14:33:51 +0300, leo <l...@clw-online.de> wrote:

Hi,
while I was reading the language specification I stumbled across the following problem:

Tuple!(int, int) x = Tuple!(1, 2);  // doesn't compile
Tuple!(int, int) y = (1, 2);    // leads to y being (2, 2)

How can I initialize a tuple by providing another tuple?

I know it's still possible to assign each element of the tuple separately, I'm just wondering what sense it makes to define an expression (1, 2) to be 2 of type int rather than (1, 2) of type (int, int).
Is there any case in which this would provide an advantage?

Leo

You have two mistakes, one for each lines of code :)
But you were close to right solutions.

1) Tuple!(int, int) x = Tuple!(1, 2);  // doesn't compile

Of course it doesn't, don't confuse type definition with a constructor call. In this case, 
"Tuple!(int, int)" is a type, and so is "Tuple!(1, 2)" (although template 
parameters are invalid; types are expected, not expressions). We could rewrite this line as follows:

A x = B; // where A and B are type aliases

What you need here is a constructor call:

A x = A(1, 2);

or

Tuple!(int,int) x = Tuple!(int,int)(1, 2);


2) Tuple!(int, int) y = (1, 2);    // leads to y being (2, 2)

Here is another quest - what does the following lines do?

int x =  1, 2;
int y = (1, 2);

They are absolutely the same and both initialize variables to 2, that's the way 
comma expression works (it evaluates all the expression and returns result of 
last expression).

Same here:

"Tuple!(int, int) y = (1, 2);" == "Tuple!(int, int) y = 2;"

If you want per-member struct initialization, you should use curly braces 
instead:

Tuple!(int, int) y = {1, 2};

Example for better understanding:

struct Foo {
   int i;
   float f;
   string s;
}

Foo foo = { 42, 3.1415f, "hello" };

Reply via email to