std::any
and auto
are completely different constructs.
std::any
is a container type that can hold an object of any type:
std::any a = 42; // a holds an int, but type is std::any
a = std::string{"hi"}; // ok, a holds a string now
The type of the object held by std::any
can change during the execution of the program.
auto
is a keyword that designates a placeholder type. The type of a variable with auto
is the type of the value used to initialize the variable:
auto a = 42; // a is int, for the entirety of the program
a = std::string{"hi"}; // error, a has type int
This type is determined statically, i.e. at compile time, and can never change during the execution of the program.
These constructs are not interchangeable, and so they have different use cases, and you can't compare the pros and cons of one versus the other meaningfully.
auto
object is resolved during compilation. The type of an object help by astd::any
is not. – Blubberheadauto
. Docs forstd::any
. – Gasp