BulGali
BulGali

Reputation: 157

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?

Upvotes: 0

Views: 156

Answers (1)

JeJo
JeJo

Reputation: 32722

Your given code has a lot of typos.

However, if you fix those(here: https://godbolt.org/z/M81qnh), the error is coming from std::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)));
//                   ^^^^^^^^^^

Upvotes: 4

Related Questions