Hard
auto vs decltype(auto) in return-type deduction
What does this program print?
main.cpp
#include <iostream>
#include <vector>
class Matrix {
std::vector<int> data{1, 2, 3};
public:
auto get(size_t i) { return data[i]; } // (1) auto
decltype(auto) getRef(size_t i) { return data[i]; } // (2) decltype(auto)
};
int main() {
Matrix m;
m.getRef(0) = 100; // modify through (2)
std::cout << m.getRef(0) << "\n";
auto copy = m.get(0); // read through (1)
copy = 999; // modify the local copy only
std::cout << m.get(0) << "\n";
}81 of 117 in C++