13

C++17 introduces the object container std::any, based on the boost library boost::any. My question is: Is the standardized any equivalent to the boost version, or are there differences?

A similar question has been posted about variant, and some differences exist in that case, but I could not find references about any.


EDIT: A difference I could see is the availability of the methods emplace. More than a difference in the API I'm interested to the differences between the behavior and the guarantees. For instance, different allocations would be significant for me.

Spiros
  • 2,156
  • 2
  • 23
  • 42

1 Answers1

6

I'm interested to the differences between the behavior and the guarantees.

There aren't any behavioral differences; not really. They both have the same requirements on the ValueType (copy-constructible, and a destructor that doesn't emit exceptions). They both provide the same operations on the values they store, with pretty much identical exception guarantees.

The principle difference is that boost::any's implementation at present doesn't implement small object optimization, while std::any implementations may provide it.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Thanks for the answer. An [answer in the linked question](https://stackoverflow.com/a/41887519/1900563) says that boost never applies small object optimization. That answer is of year old. Did the boost implementation change I the meantime? – Spiros Mar 14 '18 at 14:31
  • `boost::any` does not implement small object optimization. – Andrey Semashev Mar 16 '18 at 09:28
  • Thanks for editing the answer. Do standard libraries implement small object optimizations, that you know? – Spiros Mar 16 '18 at 14:12