This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
So, I have an std::variant containing a number of different types. In my code I'm trying to (1) retrieve a type from the std::variant via std::get; (2) copy the item; and (3) store it in an std::vector as a std::string_view.
I discovered earlier the (very) surprising and annoying behavior that std::get returns references, not copies. Which is all nice until you go to store said references somewhere, and then they start changing on you and causing unexpected program behavior. I'm unsure if declaring the vector as std::vector<const std::string_view>
would solve the problem, particularly since std::string_view
is already a read-only view of a string, and I don't want my vector storing references like this. Is there a way I can force std::get to copy items it retrieves, or a way I can do it myself?
Subreddit
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...