Let me have a custom wrapper container. I want to use it like this:
double d = 3.14;
MyContainer<std::vector<int>> pointer = new std::vector<int>();
MyContainer<std::string> rvalue = std::string("foo");
MyContainer<int> rvalue2 = 5 + 8;
MyContainer<double> lvalue = d;
I don't want to store copies of rvalues (a reference is OK). Rvalue references allow me to do like this:
std::string string1 = "foo";
std::string string2 = "bar";
std::string&& string3 = string1 + string2;
string3 += "test";
Basically I want to extend rvalues' lifetime to my container's lifetime. However when I do this:
template<class T>
class MyContainer {
public:
MyContainer(T&& obj) : object(obj) {}
T&& object
...
};
...
MyContaier<std::string> container = MyContainer(std::string("foo"));
I get an error (cannot bind 'std::string' lvalue to 'std::string&&'). The example is slightly different, but I just want to understand a general idea. How can I avoid this?