mercredi 2 décembre 2015

Is std::type_index safe across DLLs

Let's say that I have a main DLL where there's a class like this:

class Test
{
public:

    typedef std::unordered_map< std::type_index, int > Map;

    template < typename T > void SetValue(int val)
    {
        SetValue(std::type_index(typeid(T)), val);
    }

    template < typename T > int GetValue()
    {
        return GetValue(std::type_index(typeid(T)));
    }

protected:

    // Defined in .cpp file
    void SetValue(const std::type_index & idx, int val)
    {
        m_Map[idx] = val;
    }

    // Defined in .cpp file
    int GetValue(const std::type_index & idx)
    {
        Map::const_iterator itr = m_Map.find(idx);

        if (itr != m_Map.cend())
        {
            return itr->second;
        }

        return 0;
    }

private:

    Map m_Map;
};

And I share an instance of that class through several DLLs. And in one of the DLLs I set some values like this:

template < typename T > struct Dummy
{

};

void InitFunc(Test * t)
{
    t->SetValue< Dummy<int> >(32);
    t->SetValue< Dummy<char> >(10);
    t->SetValue< Dummy<float> >(27);
}

And in another DLL I attempt to get those values using the same Dummy type. Would I get those same values or 0?

Aucun commentaire:

Enregistrer un commentaire