lundi 28 septembre 2015

Declaring member reference variables in the same line

I wrote these apparently harmless lines inside my code just for naming convenience:

struct Foo{
    double vec[2];
    double& a=vec[0],b=vec[1];
};

But for some reason the variable "a" works fine and "b" does not ( it returns garbage). This is strange since it seemed to be a valid way to declare them. I then tested some alternate ways to do it:

#include <iostream>
using namespace std;
struct Foo{
    double vec[2];
    double& a=vec[0],b=vec[1]; //does not work

    double& c=vec[0];
    double& d=vec[1]; //works

    double ev,fv;
    double& e=ev,f=fv; // does not work

    double& g,h; // does not work
    Foo() : g(vec[0]) ,h(vec[1]){};

    void bar(){
        vec[0]=1;
        vec[1]=2;
        ev=1;
        fv=2;
        cout<<"a: "<<a<<endl;
        cout<<"b: "<<b<<endl;    
        cout<<"\nc: "<<c<<endl;
        cout<<"d: "<<d<<endl;
        cout<<"\ne: "<<e<<endl;
        cout<<"f: "<<f<<endl;        
        cout<<"\ng: "<<g<<endl;
        cout<<"h: "<<h<<endl;

        double& i=vec[0], j=vec[1]; //works
        cout<<"\ni: "<<i<<endl;
        cout<<"j: "<<j<<endl;
    }
};

int main(int, char **) {
    Foo X;
    X.bar();

    double vec[2];
    vec[0]=1;
    vec[1]=2;
    double& k=vec[0],l=vec[1];//works
    cout<<"\nk: "<<k<<endl;
    cout<<"l: "<<l<<endl;
}

In summary, the pairs (a,b), (e,f) and (g,h) don't work properly in the same way (i.e. the second variable returns some garbage). I know this could be easily avoided by writing like (c,d), but this whole behaviour is puzzling, specially because (a,b) fails while (i,j) and (k,l) work, even though the main difference seems to be where the declaration takes place. Why is that so?

Note: C++11 standard. Using -pedantic -Wall -Wextra in g++ did not throw any warnings

Aucun commentaire:

Enregistrer un commentaire