小编Bfy*_*uvf的帖子

std::any_cast 不需要原始对象的类型

是否可以std::any_cast在不放入第一个模板参数(所覆盖的对象的类型any)的情况下使用?我尝试使用any_cast<decltype(typeid(toCast).name())>但没有成功。

还尝试从一开始就存储对象类型,但这也不起作用,因为变量无法存储类型。

c++ casting std stdany anycast

3
推荐指数
1
解决办法
1365
查看次数

无法使用 std::any 计算容器中的项目

这个脚本

#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)

抱歉,如果这听起来有点愚蠢

c++ unordered-map ostream stdany

1
推荐指数
1
解决办法
263
查看次数

标签 统计

c++ ×2

stdany ×2

anycast ×1

casting ×1

ostream ×1

std ×1

unordered-map ×1