我想了解为什么以下代码使用 std::pair 构造函数有效:
代码:
std::multimap< std::type_index, void * > mm;
mm.insert(std::pair<std::type_index, void *>(typeid(int), 0));
并在使用 std::make_pair 时给出编译错误:
错误:
C2248 'type_info::type_info' cannot access private member declared in class 'type_info'
代码:
std::multimap< std::type_index, void * > mm;
mm.insert(std::make_pair(typeid(int), 0));
有什么线索吗?谢谢 !
最佳答案
typeid
的返回类型是 std::type_info
对象,它没有定义构造函数。作为make_pair
从传入的参数推导出其输出对的模板参数,它推导出std::pair<std::type_info, int>
.由于上述原因,它无法创建所需的对。
您的另一行创建了具有显式模板参数的对:std::pair<std::type_index, void *>
.这一次,您正在创建一个 std::type_index
对象,它确实有一个构造函数,它接受一个 std::type_info
- 正是你给它的。所以没问题。
你的 make_pair
如果还给它明确的模板参数,该行将编译:
mm.insert(std::make_pair<std::type_index, void *>(typeid(int), 0));
关于c++ - Multimap 使用 std::make_pair 与 std::pair 构造函数插入键类型信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37745498/