简体   繁体   中英

std::move of an object to a std::shared_ptr

Given the following templated class:

template<typename Container>
Class A
{
public:
    A() : {}

    bool push(std::shared_ptr<Container> container) 
    {
        ptr_vec.emplace_back(container)
    }

    void load(Container c) 
    {
        push(std::make_shared((Container)std::move(c));
    }

private:
    std::vector<std::shared_ptr<Container>> ptr_vec;
};

and the following code in main.cpp :

A<std::string> my_A {};
my_A.load("Hello");

I get the following error:

error: no matching function for call to 'make_shared(std::__cxx11::basic_string<char>)'

Can anyone shed some light regarding the error, and how to fix it?

Your given code has a lot of typos.

However, if you fix those(here: https://godbolt.org/z/M81qnh ), the error is coming fromstd::make_shared function, which is a templated function. It needs the first argument to be explicitly specified

push(std::make_shared<Container>(std::move(c)));
//                   ^^^^^^^^^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM