When I do this:
std::vector hello;
Everything works great. However, when I make it a vector of references instead:
Use std::reference_wrapper like this:
#include
#include
#include
#include
int main()
{
std::string hello = "Hello, ";
std::string world = "everyone!";
typedef std::vector> vec_t;
vec_t vec = {hello, world};
vec[1].get() = "world!";
std::cout << hello << world << std::endl;
return 0;
}
Demo
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::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 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.