← Quiz: Move Semantics and rvalue References: all 10 questions
Quiz: Move Semantics and rvalue References · Question 7 of 10
What does this print? (code)
What does this print?
#include <iostream>
#include <vector>
using namespace std;
struct T {
T(int) { cout << "B"; }
T(const T &) { cout << "C"; }
T(T &&) noexcept { cout << "M"; }
};
int main() {
vector<T> v;
v.reserve(3);
v.push_back(T(1));
v.emplace_back(2);
T t(3);
v.push_back(t);
return 0;
}
- A.BMBMBC
- B.BCBBC
- C.BMBBC
- D.BMBMBM
Answer & explanation
Correct answer: BMBBC
push_back(T(1)) builds then moves (BM), emplace_back builds in place (B), and t is built (B) then copied because it is an lvalue (C).
Can you get the rest right without peeking?
Take the timed Quiz: Move Semantics and rvalue References quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →