jeudi 25 février 2016

will cast to same type create temporal object? [duplicate]

This question already has an answer here:

This Code

#include <iostream>
using namespace std;

void f(int& a, const int& b){
    a = 0;
    a = b;
}

int main(){
    int a = 1;
    f(a, (int)a);
    cout << a;
    return 0;
}

output:

1 (with g++(4.8.1) with -std=c++11)
0 (with visual studio 2013)

I think the temporal object should be created by (int)a and pass to f.
So if I set a to 0, b shouldn't change it's value, but this is obviously not what VC++ does.

  • Is this implementation-defined or one of the compiler has bug

Aucun commentaire:

Enregistrer un commentaire