this post was submitted on 19 Apr 2024
18 points (100.0% liked)

C++

1773 readers
10 users here now

The center for all discussion and news regarding C++.

Rules

founded 1 year ago
MODERATORS
 

In c++17, std::any was added to t he standard library. Boost had their own version of "any" for quite some time before that.

I've been trying to think of a case where std::any is the best solution, and I honestly can't think of one. std::any can hold a variable of any type at runtime, which seems incredibly useful until you consider that at some point, you will need to actually use the data in std::any. This is accomplished by calling std::any_cast with a template argument that corresponds to the correct type held in the std::any object.

That means that although std::any can hold a type of any object, the list of valid objects must be known at the point that the variable is any_cast out of the std::any object. While the list of types that can be assigned to the object is unlimited, the list of types that can be extracted from the object is still finite.

That being said, why not just use a std::variant that can hold all the possible types that could be any_cast out of the object? Set a type alias for the std::variant, and there is no more boilerplate code than you would have otherwise. As an added benefit, you ensure type safety.

you are viewing a single comment's thread
view the rest of the comments
[–] herzenschein@pawb.social 2 points 7 months ago* (last edited 7 months ago)

I remember once researching when to use variant and any, and coming up with https://stackoverflow.com/questions/56303939/c-stdvariant-vs-stdany. The naïve summary being:

any is a dressed-up void*. variant is a dressed-up union.

So you'd use std::any for similar reasons to void* (that other commenters already mentioned) while getting some advantages. In that sense it's kinda similar to using a std::span for pointer arithmetic instead of actual C-style pointer arithmetic, it makes a necessary evil safer to do.