Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
`std::type_index` across shared libraries

I have noticed that, at least under c++17 ISO with g++, std::type_index seems to reliably match equality in the case of polymorphic dynamically loaded classes through dlsym. This seems to enable a nice little runtime validation of interfaces before instantiating plugin classes. For example:

template<typename T>
std::shared_ptr<T> CreateImplOf(std::string pluginName, std::string className)
{
  auto it = m_pluginDict.find(pluginName);
  if(it == m_pluginDict.end())
    return nullptr;
  auto& plug = it->second;
  const std::type_index& iface_info = plug.GetInfo()->getInterfaceTypeInfo(className.data());
  if (iface_info == std::type_index(typeid(T))) {
    std::cout << "SUCCESS! type " << iface_info.name() << " match impl interface... " << std::endl;
  } else {
    std::cout << "FAILURE! type " << typeid(T).name() << " DOES NOT match impl interface " << iface_info.name() << "... " << std::endl;
    throw std::exception{};
  }
  void* pObj = plug.CreateInstance(className);
  return std::shared_ptr<T>(reinterpret_cast<T*>(pObj));

But since I have the hunch this behavior is not defined in the standard, I wonder how much trust should we put in this runtime check

In an ideal world, equality of std::type_index between two interfaces implies ABI compatibility between them, such that invoking virtual methods through any of the two interfaces would be well defined, but almost positive we don't live anywhere near that ideal world, but exactly what expectations can one have from having this runtime check to pass? can we assume this to work on the same compiler version for the same exact class definition only? or not even that in general?

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda