Linked Questions
15 questions linked to/from Does this code from "The C++ Programming Language" 4th edition section 36.3.6 have well-defined behavior?
114 votes
3 answers
23k views
What are the evaluation order guarantees introduced by C++17?
What are the implications of the voted in C++17 evaluation order guarantees (P0145) on typical C++ code? What does it change about things like the following? i = 1; f(i++, i) and std::cout << f(...
122 votes
5 answers
18k views
C++ execution order in method chaining
The output of this program: #include <iostream> class c1 { public: c1& meth1(int* ar) { std::cout << "method 1" << std::endl; *ar = 1; return *this; ...
75 votes
3 answers
5k views
Assignment in C++ occurs despite exception on the right side
I have some (C++14) code that looks like this: map<int, set<string>> junk; for (int id : GenerateIds()) { try { set<string> stuff = GetStuff(); junk[id] = stuff; ...
18 votes
4 answers
6k views
Order of evaluation of assignment statement in C++
map<int, int> mp; printf("%d ", mp.size()); mp[10]=mp.size(); printf("%d\n", mp[10]); This code yields an answer that is not very intuitive: 0 1 I understand why it happens - the left side of ...
22 votes
1 answer
2k views
What should the result be when assigning a variable to a reference to itself, in-between modified and then returned by a function call?
#include <iostream> int& addOne(int& x) { x += 1; return x; } int main() { int x {5}; addOne(x) = x; std::cout << x << ' ' << addOne(x); } I'm ...
20 votes
2 answers
909 views
Sequence Point ambiguity, undefined behavior?
Today I came across some code that exhibits different behavior on clang++ (3.7-git), g++ (4.9.2) and Visual Studio 2013. After some reduction I came up with this snippet which highlights the issue: #...
8 votes
3 answers
1k views
How to catch undefined behaviour in function argument initialization
The following code works in clang++, but crashes spectacularly in g++ #include<vector> #include<iostream> template<class Iterator> double abs_sum(double current_sum, Iterator it, ...
2 votes
4 answers
2k views
How do char arrays and their pointers work in c++ exactly?
I am a beginner student in c++ and there is one thing I cannot understand when working with character arrays: So, I know that pointers are essentially variables that "point" to the memory address of ...
9 votes
2 answers
260 views
Unordered function evaluation for functions returning void
Is there a way in C and C++ to cause functions returning void to be evaluated in unspecified order? I know that function arguments are evaluated in unspecified order so for functions not returning ...
7 votes
2 answers
276 views
Order of evaluation in chain invocation in C++
Let's say we have class A: class A { public: A& func1( int ) { return *this; } A& func2( int ) { return *this; } }; and 2 standlone functions: int func3(); int func4(); now in this ...
3 votes
1 answer
507 views
C++ evaluation order and sequenced before and compiler reorder?
I've read from Order of evaluation, and I don't understand it well. Does the order mean the execution order in run time or just the logic order in the source code? Let's see a code snippet as below: ...
8 votes
1 answer
323 views
C++ entangled shared pointer
I have found code below in "The C++ programming language, 4th edition", chapter 17.5.1.3 struct S2 { shared_ptr<int> p; }; S2 x {new int{0}}; void f() { S2 y {x}; // ‘‘...
1 vote
1 answer
658 views
Data Structure(s) Allowing For Alteration Through Iteration and Random Selection From Subset (C++)
Given a fixed-sized array A of objects, suppose a much smaller subset of these objects meet a certain criteria B. There are three tasks I want to complete with approximately equal frequency: I want ...
2 votes
1 answer
186 views
Calling an argument-permutation-invariant function f(i++, i++)
Suppose I have a function foo(x, y, z) that is invariant w.r.t. all permutations of its arguments. I also have an iterator it, such that the iterators it, it + 1 and it + 2 can be dereferenced. Is it ...
0 votes
1 answer
67 views
How to make that : Objet.getsObject().setssObject() [duplicate]
This is my first question on this forum, and i expect this will not be a dummy one. I searched the answer everywhere but can't find it. Feel free to redirect me if the question is already answered ...