Consider the following code:
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> using namespace std; int main() { int n = 10; while(n --> 0) cout << "n: " << n << endl; } |
Should it compile? If your answer is yes, what should be its output?
Consider the following code:
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> using namespace std; int main() { int n = 10; while(n --> 0) cout << "n: " << n << endl; } |
Should it compile? If your answer is yes, what should be its output?
Some men just want to watch the world burn. Some of them are teacher assistants. And some of them give tests like this to an intro class. Sure, it’s not very difficult (except for 1e, which requires deeper understanding of the language), but it’s really, really tricky.
Well, check for yourself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <atomic> #include <chrono> #include <iomanip> #include <iostream> #include <string> #include <thread> #include <vector> using namespace std; int main() { auto known_cores = thread::hardware_concurrency(); auto cores = max(known_cores-1,decltype(known_cores){1}); volatile bool done = false; auto f = [&](){ while(!done); }; vector<thread> v; for(decltype(cores) i{0}; i < cores-1; ++i){ v.push_back(thread{f}); } string temp; getline(cin,temp); done = true; for(auto&& t : v){ t.join(); } return 0; } |
It’s been a while since I posted anything, so… here I am, still alive, doing science, and a little busy. That said, just a few hours ago I stumbled upon two interesting C and C++ quizzes and, despite my own score being rather poor, I’d like to share. Without further ado, links:
I have recently been asked if it’s possible to write a legit C program that wouldn’t compile in C++ mode. Obviously, my answer was positive, but the question made me wonder just how many incompatibilities there are between these languages, and how many of these I can cram into a single test case? I’ve decided to try my hand at writing such test case, and after a while of research I ended up with a fairly decent attempt.