Advanced Minds

  • Decrease font size
  • Default font size
  • Increase font size
  • default color
  • red color
  • green color
FireBoard
Welcome, Guest
Please Login or Register.    Lost Password?
c++ precedence Precedence in C and C++ (1 viewing) (1) Guests
Go to bottom Post Reply Favoured: 0
TOPIC: c++ precedence Precedence in C and C++
#8298
c++ precedence Precedence in C and C++  
++x = 55; is not legal C because, in C, you can't apply assignment to an rvalue, and the _expression_ ++x gives an rvalue, not an lvalue. In C++, it's legal (but may result in undefined behaviour) because you can overload ++ to do what you want, so it's unnecessarily restrictive to ban the idiom in C++. According to Dr. Bjarne Stroustrup it would not cause any undefined behaviour. Mr Stroustrup's opinion is certainly valuable - but what does the C++ Standard say on the matter?
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#8299
c++ precedence Precedence in C and C++  
Richard Heathfield < This e-mail address is being protected from spam bots, you need JavaScript enabled to view it wrote in message <news: This e-mail address is being protected from spam bots, you need JavaScript enabled to view it <snip ++x = 55; is not legal C because, in C, you can't apply assignment to an rvalue, and the _expression_ ++x gives an rvalue, not an lvalue. In C++, it's legal (but may result in undefined behaviour) because you can overload ++ to do what you want, so it's unnecessarily restrictive to ban the idiom in C++. According to Dr. Bjarne Stroustrup it would not cause any undefined behaviour. Mr Stroustrup's opinion is certainly valuable - but what does the C++ Standard say on the matter? I don't see anything about undefined behaviour in the standard, in fact from the standard, it appears quite unambiguous. operator= binds more tightly than operator++, so the code ++x = 55; should be equivalent to this: ++x; x=55; But if x is a user defined type, then it's anyones guess what the above code actually does.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#8300
Andrew Koenig (Visitor)
Click here to see the profile of this user
Birthdate:
c++ precedence Precedence in C and C++  
Donovan I don't see anything about undefined behaviour in the standard, in fact Donovan from the standard, it appears quite unambiguous. operator= binds more Donovan tightly than operator++, so the code Donovan ++x = 55; Donovan should be equivalent to this: Donovan ++x; Donovan x=55; The trouble is that  ++x = 55  modifies x twice between sequence points.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#8301
Dann Corbit (Visitor)
Click here to see the profile of this user
Birthdate:
c++ precedence Precedence in C and C++  
to ban the idiom in C++. According to Dr. Bjarne Stroustrup it would not cause any undefined behaviour. Mr Stroustrup's opinion is certainly valuable - but what does the C++ Standard say on the matter? I don't see anything about undefined behaviour in the standard, in fact from the standard, it appears quite unambiguous. operator= binds more tightly than operator++, so the code ++x = 55; should be equivalent to this: ++x; x=55; But if x is a user defined type, then it's anyones guess what the above code actually does. In the C++ language, you still have two modifications of a single _object_ without a sequence point.  Therefore, we have a very clear case of undefined behavior. From ŠISO/IEC ISO/IEC 9899:1999 (E): 6.5 _expression_s 1 An_expression_ is a sequence of operators and operands that specifies computation of a value, or that designates an _object_ or a function, or that generates side effects, or that performs a combination thereof. 2 Between the previous and next sequence point an _object_ shall have its stored value modified at most once by the evaluation of an _expression_. Furthermore, the prior value shall be read only to determine the value to be stored.70) Foonote 70) This paragraph renders undefined statement _expression_s such as i = ++i + 1; a[i++] = i; while allowing i =i +1; a[i] = i; Hence, it is clear that:  ++x = 55; produces undefined behavior in the C++ language.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#8302
James Dennett (Visitor)
Click here to see the profile of this user
Birthdate:
c++ precedence Precedence in C and C++  
But if x is a user defined type, then it's anyones guess what the above code actually does. If you overload prefix ++ then it does what you want; in the case of a UDT, there is sequence point after the ++ function call, and the transformation to ++x; x=55; is valid.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop
sprawdz autoryzacje sprawdz autoryzacje brak autoryzacji brak autoryzacji brak autoryzacji
naruto online games watch nba games online polaris world news releases diving courses