Is std :: type_index safe in DLL

Say I have a main DLL where there is 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 pass an instance of this class through several DLLs. And in one of the DLLs I set 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 am trying to get these values ​​using the same type Dummy. Am I getting the same values ​​or 0?

+4
source share
2 answers

, ODR - . , 'Dummy' , --:). undefined, - , .

+1

"" .

@SergeyA , std::type_info , , DLL .

std::type_info , , (, , ) , , .

, CppReference

type_index - std:: type_info, . type_info '

, DLL.

, std::type_index, , - , .

: ? , . , std ( STL) DLL.

, - :

  • (debug vs release Windows).
  • API

, , , , .

+3

Source: https://habr.com/ru/post/1618320/


All Articles