If I have a function that returns a reference to an instance of a class that I don't have control over its source, say list<int>
:
list<int>& f();
I want to ensure that its value is only assigned to another reference, e.g.:
list<int> &a_list = f();
If the user were instead to do:
list<int> a_list = f(); // note: no '&', so the list is copied
I want it to be a compile-time error since the user would be manipulating only a copy of the list and not the original list (which is never what is intended/wanted for my application).
Is there any way to prevent copy-construction and assignment in the above (say via some kind of "wrapper" class)?
Ideally, if some wrapper class were to be used, say wrapper<T>
, I'd like it to work for objects of any type T
.
Yes, I know that for a class that I do have control over, I can simply make the copy-constructor and assignment operator private
like:
class MyClass {
public:
// ...
private:
MyClass( MyClass const& );
MyClass operator=( MyClass const& );
};
to forbid copy-construction and assignment; but, as shown above, I want to do this for, say, std::list
for which I can not simply make the copy-constructor and assignment operator private
.