I've been browsing for a while in search of an answer but I don't seem to find it. So I decided to specifically ask the question here.
I've been trying to use something like this (and variants hereof):
struct NonCopyable
{
NonCopyable() { };
NonCopyable(const NonCopyable& other) = delete;
NonCopyable(NonCopyable&& other) { };
};
struct Host
{
Host(NonCopyable&& nc) : m_nc(nc) { }
NonCopyable m_nc;
};
to achieve this:
Host h(NonCopyable());
In other words, I'd like to construct NonCopyable inside the m_nc. Is this at all possible?
Note that nc
is an lvalue as a named parameter, you need to convert it to rvalue, e.g. via std::move
Host(NonCopyable&& nc) : m_nc(std::move(nc)) { }
EDIT
There's a most vexing parse issue. Host h(NonCopyable());
is not a variable definition, but a function declaration; which declares a function named h
, which returns Host
and takes an unnamed parameter with type of function pointer (which takes nothing and returns NonCopyable
).
Just change it to Host h{NonCopyable()};
.