Thread: Must the order of execution(in this case) be left to right?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Must the order of execution(in this case) be left to right?

    Suppose I have a statement like :
    Code:
    a.send_buffer.front().p.get_main_data();
    I'm not getting any trouble with it, but

    Is is better to put parentheses ?
    or is it sufficiently defined that the order should be followed ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I do not think that there is any other way to evaluate the expression.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by laserlight View Post
    I do not think that there is any other way to evaluate the expression.
    Why not?
    If a similar expression contains ->* or .* or similar operators related to pointers, couldn't a function encountered later in the expression return a pointer to a member of an object encountered earlier ? I think I have seen examples like that in the Qt Documentations .

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    Why not?
    If a similar expression contains ->* or .* or similar operators related to pointers, couldn't a function encountered later in the expression return a pointer to a member of an object encountered earlier ? I think I have seen examples like that in the Qt Documentations .
    Once a function's arguments have been evaluated, a sequence point is introduced.

    Another related way to look at it: consider that we could write x.y() as y(x), if we pretend that the object on which the member function operates was passed (by reference) as the first argument to the function. This way, we could re-write a.send_buffer.front().p.get_main_data() as get_main_data(front(a.send_buffer).p). Clearly, in this form, adding parentheses isn't going to help.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Operators in C and C++ - Wikipedia, the free encyclopedia

    Dot has the same precedence as () (read: the only other operator in the example) so the expression is evaluated from left to right. That's what associativity is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Relying on order of execution
    By 127.0.0.1 in forum C Programming
    Replies: 5
    Last Post: 06-08-2011, 01:34 AM
  2. order of execution
    By manzoor in forum C++ Programming
    Replies: 15
    Last Post: 08-13-2009, 01:14 PM
  3. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  4. expression execution order
    By _Elixia_ in forum C Programming
    Replies: 3
    Last Post: 10-02-2003, 04:01 PM
  5. randomizing order of execution of function
    By y2jasontario in forum C Programming
    Replies: 2
    Last Post: 04-03-2002, 07:50 PM