[英]unordered_map with std::any values cannot any_cast string
I'm new to C++ and I was studying about std::unordered_map
and std::any
. 我是C ++的新手,正在研究
std::unordered_map
和std::any
。 I've created a sample demo bellow which generates some dummy data and then they get inserted into the map. 我创建了一个示例演示波纹管,它会生成一些虚拟数据,然后将它们插入到地图中。
After that (in the commented out code) I print out the values using any_cast
with success. 之后(在注释掉的代码中)我成功地使用
any_cast
打印出值。
However at the bottom as you may see I try to retrieve a specific key with 0 success. 但是,您可能会看到在底部,我尝试检索成功为0的特定键。 The error I'm getting is
Bad any_cast
and the casting is exactly the same as the code I used for printing (which is currently commented out). 我得到的错误是
Bad any_cast
,并且转换与我用于打印的代码完全相同(当前已注释掉)。
I'm sorry if this is a silly mistake but I'm quite new. 如果这是一个愚蠢的错误,我很抱歉,但是我很新。 Thank you in advance.
先感谢您。
#include <iostream>
#include <string>
#include <any>
#include <map>
#include <unordered_map>
#include <time.h>
#include <Windows.h>
std::unordered_map<std::string, std::any> map = {};
int main() {
unsigned long started = clock();
const std::string prefix = "key";
for (int i = 0; i < 1000000; i++) {
const std::string key = "key" + std::to_string(i);
map.insert_or_assign(key, i);
}
std::cout << "Data inserted after: " << (clock() - started) << "ms" << std::endl;
system("pause");
started = clock();
/*
for (const auto& item : map) {
try {
//std::cout << item.second.type().name() << std::endl;
if (item.second.type() == typeid(int)) {
std::cout << std::any_cast<const int>(item.second) << std::endl;
}
else if (item.second.type() == typeid(float)) {
std::cout << std::any_cast<const float>(item.second) << std::endl;
}
else if (item.second.type() == typeid(double)) {
std::cout << std::any_cast<const double>(item.second) << std::endl;
}
else if (item.second.type() == typeid(long)) {
std::cout << std::any_cast<const long>(item.second) << std::endl;
}
else if (item.second.type() == typeid(char const *)) {
std::cout << std::any_cast<char const *>(item.second) << std::endl;
}
else if (item.second.type() == typeid(std::string)) {
std::cout << std::any_cast<const std::string>(item.second) << std::endl;
}
else {
std::cout << item.first << " has an unhandled value type of " << item.second.type().name() << std::endl;
}
}
catch (const std::bad_any_cast& err) {
std::cerr << err.what() << std::endl;
}
}
std::cout << "Map iterated after: " << (clock() - started) << "ms" << std::endl;
*/
try {
auto value = std::any_cast<char const *>(map["key8745"]);
std::cout << "Key " << value << " retrieved after: " << (clock() - started) << "ms" << std::endl;
}
catch (const std::bad_any_cast &err) {
std::cerr << err.what() << std::endl;
}
system("pause");
return 0;
}
All the values you insert are int
s, but you're trying to cast the single value at the bottom to char const*
; 您插入的所有值都是
int
,但是您试图将底部的单个值强制转换为char const*
; it's not a pointer of any kind, and it looks like any_cast
is sufficiently type safe to reject that attempt to cast (a good thing, since dereferencing a pointer to memory address 8745
is likely to segfault). 它不是任何类型的指针,并且似乎
any_cast
是足够类型安全的,可以拒绝进行any_cast
尝试(这是一件好事,因为将指针指向内存地址8745
引用很可能会发生段错误)。 You're type checking in the earlier code and presumably selecting the correct cast there. 您在较早的代码中进行类型检查,并大概在那里选择了正确的转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.