sequence-points

Are there any situations where code would have a sequence point in c++11 but not c++03?

萝らか妹 提交于 2019-12-04 18:54:18
问题 Now that the new c++11 standard has made changes in how sequence points are described I'm trying to find out exactly what has been changed between c++03 and c++11. In particular, are there any situations where code that looks the same would have a sequence point in c++11 but not c++03? 回答1: There are no sequence points in C++11, rather there are sequenced before and sequenced after relations. Here are some trivial examples wherein behavior differ between C++03 and C++11 int x = 10; ++++x; //

Is there a sequence point between these assignments?

陌路散爱 提交于 2019-12-04 16:42:48
问题 Is there a sequence point between the two assignments in the following code: f(f(x=1,1),x=2); 回答1: The relevant quote from the (draft) standard [6.5.2.2, 10] is: The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call. So for your expression, the first argument (in particular the call to f ) could be evaluated before the second argument; e.g.: (x = 1, 1), f <sp

Does standard C++11 guarantee that temporary object passed to a function will have been created before function call?

我只是一个虾纸丫 提交于 2019-12-04 14:01:05
Does standard C++11 guarantee that all 3 temporary objects have been created before the beginning performe the function? Even if temporary object passed as: object rvalue-reference passed only member of temporary object http://ideone.com/EV0hSP #include <iostream> using namespace std; struct T { T() { std::cout << "T created \n"; } int val = 0; ~T() { std::cout << "T destroyed \n"; } }; void function(T t_obj, T &&t, int &&val) { std::cout << "func-start \n"; std::cout << t_obj.val << ", " << t.val << ", " << val << std::endl; std::cout << "func-end \n"; } int main() { function(T(), T(), T()

Yet another sequence point query: how does *p++ = getchar() work?

…衆ロ難τιáo~ 提交于 2019-12-04 11:58:11
§5.1.2.4.16 EXAMPLE 7 The grouping of an expression does not completely determine its evaluation. In the following fragment: #include <stdio.h> int sum; char *p; /* ... */ sum = sum * 10 - '0' + (*p++ = getchar()); the expression statement is grouped as if it were written as sum = (((sum * 10) - '0') + ((*(p++)) = (getchar()))); but the actual increment of p can occur at any time between the previous sequence point and the next sequence point (the ; ), and the call to getchar can occur at any point prior to the need of its returned value. So basically I understand this as unspecified behavior

Sequence points when calling functions in C and undefined/unspecified behaviour

隐身守侯 提交于 2019-12-04 11:24:09
I'm trying to pin down my understanding of sequence points in C -- just wanted to check something. At present, I believe that (1) is undefined whereas (2) is merely unspecified, on the basis that in (2), there are sequence points after evaluating the arguments for g and h (so we're not modifying i twice between sequence points), but the order of evaluation of the arguments of f is still unspecified. Is my understanding correct? #include <stdio.h> int g(int i) { return i; } int h(int i) { return i; } void f(int x, int y) { printf("%i", x + y); } int main() { int i = 23; f(++i, ++i); // (1) f(g(

Is (++i)++ undefined behavior?

眉间皱痕 提交于 2019-12-04 06:24:00
Is (++i)++ undefined behavior? Is it possible that the side effect of prefix increment happens after retrieving the incremented object for postfix increment to operate on? That would seem strange to me. My gut feeling says this is undefined in C++03 and well-defined in C++11. Am I right? My gut feeling says this is undefined in C++03 and well-defined in C++0x. Yes you are right. The behaviour is undefined in C++03 because you are trying to modify i more than once between two sequence points. The behaviour is well defined in C++0x because (++i)++ is equivalent to (i += 1)++ . The side effects

Assignment and sequence points: how is this ambiguous?

核能气质少年 提交于 2019-12-04 04:03:30
问题 Consider the C code a = a = a . There's no sequence point for assignment, so this code produces a warning when compiling about an undefined operation on a . What are the possible values that a could have here? It seems like a couldn't possibly change values. Is there actually undefined behavior here or are compilers just being lazy? 回答1: The rules of undefined behavior for sequence point violations do not make an exception for situations when "the value cannot change". Nobody cares whether

Does *&++i cause undefined behaviour in C++03?

我与影子孤独终老i 提交于 2019-12-03 23:55:24
In another answer it was stated that prior to C++11, where i is an int , then use of the expression: *&++i caused undefined behaviour. Is this true? On the other answer there was a little discussion in comments but it seems unconvincing. It makes little sense to ask whether *&++i in itself has UB. The deferencing doesn't necessarily access the stored value (prior or new) of i , as you can see by using this as an initializer expression for a reference. Only if an rvalue conversion is involved (usage in such context) is there any question to discuss at all. And then, since we can use the value

Why I got “operation may be undefined” in Statement Expression in C++?

偶尔善良 提交于 2019-12-03 23:40:15
to describe the problem simply, please have a look at the code below: int main() { int a=123; ({if (a) a=0;}); return 0; } I got this warning from [-Wsequence-point] Line 4: warning: operation on 'a' may be undefined my g++ version is 4.4.5 I'll appreciate whoever would explain this simple problem. btw you could find my original program and original problem in #7 in this Chinese site (not necessary) UPD1: though to change the code into ({if(a) a=0; a;}) can avoid the warning, but I recognized that the real reason of the problem may not be The last thing in the compound statement should be an

Does int a=1, b=a++; invoke undefined behavior?

自古美人都是妖i 提交于 2019-12-03 11:16:52
问题 Does int a=1, b=a++; invoke undefined behavior? There is no sequence point intervening between the initialization of a and its access and modification in the initializer for b , but as far as I can tell, initialization is not "modification" of the object; an initializer is specified to give the "initial value" of the object. Per 6.7.8 Initialization, paragraph 8: An initializer specifies the initial value stored in an object. and it seems reasonable to take "initial" as being sequenced before