Since C++17 std::any
is introduced. One can now write code like this
#include <iostream> #include <any> #include <string> int main () { const double d = 1.2; std::any var = d; const std::string str = "Hello World"; var = str; }
A double is assigned to the variable var
and than a std::string
was assigned to it.
Why has std::any
been introduced?
I think this is violating the least astonishment rule
, because I find it hard to think of a situation, where this can be used to express more clearly, what I like to express.
Can somebody give me a good example, when std::any
is beneficial.
https://gcc.godbolt.org/z/-kepOD
std::any Class in C++ any is one of the newest features of C++17 that provides a type-safe container to store single value of any type. In layman's terms, it is a container which allows one to store any value in it without worrying about the type safety.
std::any uses Small Buffer Optimization, so it will not dynamically allocate memory for simple types like ints, doubles… but for larger types it will use extra new . std::any might be considered 'heavy', but offers a lot of flexibility and type-safety.
std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.
std::string - the C++ String Class. C++ provides a simple, safe alternative to using char*s to handle strings. The C++ string class, part of the std namespace, allows you to manipulate strings safely.
When to Use
void*
as an extremely unsafe pattern with some limited use cases, std::any
adds type-safety, and that’s why it has some real use cases.
Some possibilities:
I would summarize it as classic "use when you cannot avoid it".
I can only think of non-performance-critical implementations of dynamically typed scripting languages to represent variables from the scripting world, but even that with a stretch (Boost.Spirit/example/qi/compiler_tutorial does it without, for both the parser and the runtime).
For everything else from parsers (e.g. Boost.Spirit.X3) to library APIs (e.g. ASIO) there would usually be a faster/better/more-specific alternative, as very few things are really "anything", most are more specific than that.
std::variant
and/or std::optional
for "almost any value"std::packaged_task
/ std::function
+ lambdas for "callback with arguments", which would be a case of void*
in C APIs.Specifically, I wouldn't blindly plug it as a replacement for a void*
, as it may allocate memory on the heap, which can be deadly for high performance code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With