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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…