Reputation: 1283
Here is a code snippet:
#include <iostream>
#include <array>
struct Matrix
{
struct Proxy
{
Proxy(Matrix& m) : m_ { m } {}
Proxy(Proxy&& rhs) : m_ { rhs.m_ }, a { std::move(rhs.a) }, index{rhs.index} {} // 1
Proxy operator[](int i)
{
++index;
return { std::move(*this) };
}
Proxy& operator=(int value)
{
m_.v = value;
return *this;
}
Matrix& m_;
int index = 0;
std::array<int, 1000> a;
};
Proxy operator[](int index)
{
return { *this };
}
int v = -1;
};
int main()
{
Matrix m;
const auto& q = m[1][2][3]; // 2
std::cout << q.index << std::endl;
std::cout << q.m_.v << std::endl;
}
I'm interested in behavior of temporary objects in such a case - whether my snippet violates any rules when moving from one temporary to another temporary at point 1 and is it guaranteed that temporary proxy
's lifetime will be prolongated at point 2 due binding to const reference?
Upvotes: 0
Views: 54