mardi 21 février 2017

C++11: the type of decltype((x)) and decltype((x+1)) are different?

I was thinking that decltype((x)) give the & reference type, but some experiment showed sth else:

#include<stdio.h>
int main(){
    int x = 0;
    decltype((x)) r = x;
    r = 1;
    printf("%d\n",x);

    decltype((x+1)) i = x;
    i = 2;
    printf("%d\n",x);

    decltype((1)) k = x;
    k = 3;
    printf("%d\n",x);
    return 0;
}

I was expecting that (x) and (x+1) and (1) woul all give an "int&". However the running result was

1
1
1

I expected that the running result should be 1,2,3 as each decltype retrieves reference, but seems only the 1st works, while both (x+1) an (1) gives only int, not 'int&'. Why, are (x) and (x+1) of different id-expression type?

Aucun commentaire:

Enregistrer un commentaire