可以获取对象的静态类型吗?

can one get the static type of an object?

本文关键字:静态类 类型 静态 获取 取对象      更新时间:2023-10-16

我觉得这应该是显而易见的,但我想我今天很生气。

如何让编译器为我提供事物的静态类型?

前任:

auto it = m_security_look_aside.find(strPath);
if (it == m_security_look_aside.end())
    it = m_security_look_aside.insert(it, TYPE_OF(m_security_look_aside)::value_type(strPath, InternalIsLicensed(strPath)));

m_security_look_aside是一个std::unordered_map<std::string, bool>. std::unordered_map<std::string, bool>有一个value_type类型定义。

我可以使用std::unordered_map<std::string, bool>::value_type(key,value)来构造一个属于这个无序映射的对。 但是,如何从实例转到其静态类型呢?

使用 decltype(m_security_look_aside)::value_type - 即 decltype(m_security_look_aside)可用于表示实例变量的类型。

但你在这里真的不需要它。相反,你可以做

it = m_security_look_aside.emplace_hint(it, strPath, InternalIsLicensed(strPath));

这"置换"(就地构造)正确value_type的对象,使用参数(在初始"提示"迭代器之后)作为value_type的构造函数参数。