Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
`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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda