Using std::unique_ptr inside a map as a key

后端 未结 2 1270
一向
一向 2021-01-18 13:05

I am using Visual Studio 2012. I have a map that looks like this:

std::map,std::unique_ptr

        
2条回答
  •  别那么骄傲
    2021-01-18 13:35

    Look at the template definition for std::map:

    template<
        class Key,
        class T,
        class Compare = std::less,
        class Allocator = std::allocator >
    > class map;
    

    And now lets look at how you try to instantiate it:

    std::map<
        std::string, 
        std::map<
            std::unique_ptr, 
            std::unique_ptr
        >
    > 
    listSoundContainer
    

    The problem here is that a std::unique_ptr cannot act as a key.

    What you seem trying to do is to make some kind of list of std::pair, std::unique_ptr>

    I would suggest using this instead:

    std::map<
        std::string, 
        std::list<
            std::pair<
                std::unique_ptr, 
                std::unique_ptr
            >
        >
    > 
    listSoundContainer
    

提交回复
热议问题