Suppose I have an object X
and a proxy. I want to control lifetime of the proxy so that it cannot be extended beyond temporary object, returned by a method of the X
. I can't understand if the way I did it is due to what standard allows.
class X
{
public:
struct Proxy
{
~Proxy();
/// Implicit convertion to wrapped object.
operator X &();
operator const X &() const;
/// Explicit access to wrapped object.
X & get();
const X & get() const;
private:
friend class X;
Proxy(X & x_ref);
Proxy(Proxy && other) noexcept;
Proxy(const Proxy &) = delete;
Proxy & operator = (const Proxy&) = delete;
Proxy & operator = (Proxy && other) = delete;
X & _x_ref;
};
Proxy get_proxy() {
Proxy proxy(*this);
// ...
return proxy;
}
};
Specifically I am worried about the part when an object is returned from the X::get_proxy()
method. Technically, who calls the move constructor, to move an object from the body of the method into temporary object, that is a return value of this method? Does the standard says it is done in a scope of X, so friend
specifier works correctly here?
It compiles without neither warnings nor errors with GCC 4.7-4.9 and clang 3.0-3.5. But I just need confirmation from the 3rd party that this won't change in the future, because it is a standardized behavior.
Note: this code example may seem silly, but it is a stripped, general case example. In the real world application proxy is used to expose some internal data of the X, not the reference to X.
Note: here I use move constructor since it's a future and it's time to move on and forget about old standards. But this proxy could be made with a copy constructor instead. Though this does not change the question. Except if things has changed in this scope in between c++03 and c++11/14.