是否可以std::any_cast在不放入第一个模板参数(所覆盖的对象的类型any)的情况下使用?我尝试使用any_cast<decltype(typeid(toCast).name())>但没有成功。
还尝试从一开始就存储对象类型,但这也不起作用,因为变量无法存储类型。
这个脚本
#include <iostream>
#include <unordered_map>
#include <any>
using namespace std;
int main() {
unordered_map<int, any> test;
test[5] = "Hey!";
cout << test[5];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么它不起作用?
candidate function not viable: no known conversion from 'std::__ndk1::unordered_map<int, std::__ndk1::any, std::__ndk1::hash<int>, std::__ndk1::equal_to<int>, std::__ndk1::allocator<std::__ndk1::pair<const int, std::__ndk1::any> > >::mapped_type' (aka 'std::__ndk1::any') to 'const void *' for 1st argument; take the address of the argument with &
basic_ostream& operator<<(const void* __p);
Run Code Online (Sandbox Code Playgroud)
抱歉,如果这听起来有点愚蠢