Question

Is the following code safe?

boost::any any_value;

{
    std::string s = "HelloWorld";
    any_value = s;
}

std::string ss = any_cast<std::string>(any_value);
Was it helpful?

Solution

From Boost.Any docs:

template<typename ValueType> any & operator=(const ValueType & rhs);

Makes a copy of rhs, discarding previous content, so that the new content of is equivalent in both type and value to rhs.

So yes, it's safe to do that. A copy of the string is stored, not a reference to it.

OTHER TIPS

Yes it is. boost::any takes everything by making a copy of it, assuming the type fulfills the Copyable concept.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top