3

I'll let you try to talk me out of this inheritance in a minute*, but for now I'm hoping to find out why this error happens.

Here's a stripped down example that reproduces the issue in MSVC 2015:

template<typename key_type, typename value_type>
class my_unordered_map : public unordered_map<key_type, value_type>
{
    public:
        value_type& operator[](const key_type& key)
        {
            return unordered_map<key_type, value_type>::operator[](key);
        }
};

int main()
{
    my_unordered_map<string, int> m;
    m["x"] = 42;
}

All that my_unordered_map is intending to do (so far) is override one member function and delegate the call to its overridden base member function. But I get the error is MSVC 2015:

binary '=': no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

Any thoughts why?


* Okay, so now you can try to talk me out of this inheritance. The gist is I want an unordered_map + one extra feature. But aside from that feature, I still want to retain the entire interface and implementation of unordered_map, and inheritance seems the most straightforward way to do that. And even though the language will allow it, I don't plan to switch between the two kinds of maps polymorphically, so virtual or lack of shouldn't be an issue.

Jeff M
  • 2,492
  • 3
  • 22
  • 38
  • 1
    Compiles [here](http://coliru.stacked-crooked.com/a/c27207d7dc531dce). Also [works](http://coliru.stacked-crooked.com/a/6dfb851d039474ec) with gcc. What compiler are you using? – Baum mit Augen Feb 19 '16 at 00:48
  • Hmm. Seems you're right. When I compile with gcc 4.9, it compiles fine. But when I compile with MSVC 2015, then I get the above error. – Jeff M Feb 19 '16 at 01:28

1 Answers1

1

In MSVC's implementation (probably defined by the standard?) of map and unordered_map, the alias value_type is std::pair<const key_type, mapped_type>. The key_type and value_type of std::unordered_map are somehow hiding the template type parameters in your derived class. If you rename them as, say, key_type_ and value_type_ it should work.

I'm not sure why the typedefs in unordered_map would overshadow the template parameters for the class. I think this question asks about this, and the comments seem to indicate it is a MSVC extension.

Community
  • 1
  • 1
Weak to Enuma Elish
  • 4,622
  • 3
  • 24
  • 36