Interestingly, that behavior seems consistent across compilers?
I used the code
#include <iostream>
#include <string>
using namespace std;
string string_to_int(int number)
{
switch(number){
case 0: return string("0");
case 1: return string("1");
case 2: return string("2");
case 3: return string("3");
case 4: return string("4");
case 5: return string("5");
default: return string("-1");
}
}
int main()
{
int counter = 0;
string output(string(string_to_int(counter++)) + string(string_to_int(counter++)) + string(string_to_int(counter++)) + string(string_to_int(counter++)));
cout << output;
cin.ignore();
}
and got the exact same output "3210" on gcc and msvc.
Fakeedit: I realized that string(string_to_int()) is totally redundant. Doesn't matter, though. xD
Meanwhile, the code
import java.lang.*;
public class TestIncrement
{
public static void main(String[] args)
{
int counter = 1;
System.out.println(""+counter++ + counter++ + counter++ + counter++ + counter++);
}
}
results in an output of
12345, and the very similar code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
int counter = 1;
Console.WriteLine("" + counter++ + counter++ + counter++ + counter++ + counter++);
Console.ReadLine();
}
}
has the same output.
Same with
$counter = 1;
print "" . $counter++ . $counter++ . $counter++ . $counter++ . $counter++;
Sadly, that's the extent of the languages I know that have ++
It's interesting that both gcc and msvc have the same output for the undefined behavior. Someone's guess that it had to do with the stack and writing simpler machine code is probably right...
Also, if you try to copy the C++ code into codepad, you'll find that the compiler generates an 'undefined behavior' warning. The only probable reason you'd see it if the option to treat warnings like errors is on, like codepad does o_O
Anyhow, that's the end of my rather simple testing. It was on a whim.