comma operator

Sivo Schilling i-b-p at gmx.net
Mon Mar 10 09:36:52 PDT 2008


In C/C++ comma operator allows grouping two statements where one is expected.

Example (C/C++):
-------
#include <stdio.h>

int validate(int pos)
{
    return (--pos <= 0 || --pos <= 0), pos;
}

void main()
{
  printf("validate(1) gives %d\n", validate(1));
  printf("validate(2) gives %d\n", validate(2));
  printf("validate(10) gives %d\n", validate(10));
}
-------
Output:
validate(1) gives 0
validate(2) gives 0
validate(10) gives 8

The example above now in D (DMD 1.x/2.x):
-------
import std.stdio;

int validate(int pos)
{
    return (--pos <= 0 || --pos <= 0), pos;
}

void main()
{
  writefln("validate(1) gives ", validate(1));
  writefln("validate(2) gives ", validate(2));
  writefln("validate(10) gives ", validate(10));
}
-------
Output:
validate(1) gives 1
validate(2) gives 2
validate(10) gives 10

Is this expected behaviour or if there's something I've missed ?
Any suggestions ?

Regards, Sivo.


More information about the Digitalmars-d-learn mailing list