Archived

This topic is now archived and is closed to further replies.

surprising order!

This topic is 5579 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

float getNextFloat();

// This code caused me trouble:
vec3 getNextVec3() {
    return vec3(getNextFloat(),getNextFloat(),getNextFloat());
}

// I thought that it worked similar to this:
vec3 getNextVec3() {
    vec3 r;
    r.x = getNextFloat();
    r.y = getNextFloat();
    r.z = getNextFloat();
    return r;
}
  
I thought that the two methods would give the same result. But no. Why are x, y and z values opposite order in the first getNextVec3() method? ----------- RacingTreme - for fun multiplayer racing: http://users.utu.fi/stkibr Coming soon... [edited by - stefu on September 9, 2002 12:52:26 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The order of evaluation for function (or method) parameters is undefined. If you say "f(x,y,z)" then y could be evaluated first, or it could be z, or it could be x. It depends on the compiler, and can even depend on the compiler''s optimization settings.

Share this post


Link to post
Share on other sites
Because arguments are pushed on to the stack in right-to-left order, meaning that if you use a return value from a function, that function gets called.

So, instead of making your constructor like this:

vec3::vec3(float x, float y, float z)
{

}


you should do this if you want the above code to work:

vec3::vec3(float z, float y, float x)
{

}

Share this post


Link to post
Share on other sites
quote:
Original post by daerid
Because arguments are pushed on to the stack in right-to-left order, meaning that if you use a return value from a function, that function gets called.

I''m not convinced that this behaviour is guaranteed across all compilers (ie. in the ISO/ANSI C++ standard). I thought it was undefined. Do you have evidence of this?


[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites