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

This is a discussion on Must the order of execution(in this case) be left to right? within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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 ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    I do not think that there is any other way to evaluate the expression.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    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, 06:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21