TL; DR
다음 std::reference_wrapper과 같이 사용하십시오 .
#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
긴 대답
으로 표준 제안 , 표준 컨테이너 X유형의 객체를 포함 T, T수 있어야 Erasable에서 X.
Erasable 다음식이 잘 구성되어 있음을 의미합니다.
allocator_traits<A>::destroy(m, p)
A컨테이너의 할당 자 유형이고 m할당 자 인스턴스이며 p유형의 포인터입니다 *T. 정의는 여기 를 참조 하십시오Erasable .
기본적 std::allocator<T>으로 벡터의 할당 자로 사용됩니다. 기본 할당 자에서 요구 사항은 유효성과 같습니다 p->~T()(참고 T는 참조 유형이며 참조에 대한 p포인터 임). 그러나 참조에 대한 포인터는 불법 이므로 표현식이 제대로 구성되지 않았습니다.