TL; DR
Use std::reference_wrapper like this:
#include <functional> #include <string> #include <vector> #include <iostream> int main() { std::string hello = "Hello, "; std::string world = "everyone!"; typedef std::vector<std::reference_wrapper<std::string>> vec_t; vec_t vec = {hello, world}; vec[1].get() = "world!"; std::cout << hello << world << std::endl; return 0; }
Demo
Long answer
As standard suggests, for a standard container X containing objects of type T, T must be Erasable from X.
Erasable means that the following expression is well formed:
allocator_traits<A>::destroy(m, p)
A is container's allocator type, m is allocator instance and p is a pointer of type *T. See here for Erasable definition.
By default, std::allocator<T> is used as vector's allocator. With the default allocator, the requirement is equivalent to the validity of p->~T() (Note the T is a reference type and p is pointer to a reference). However, pointer to a reference is illegal, hence the expression is not well formed.