new and std::make_shared

closed account (ywbpSL3A)
I was trying to check whether an entry had already been created in a std::map and, if not, allocate new memory for said entry. The thing I don't understand is why new works, but std::make_shared doesn't. Do I have to supply a custom deleter when using std::make_shared?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <memory>
#include <set>
#include <map>

int main() {
    std::map<std::string, std::shared_ptr<std::set<int>>> dict;

    auto& p = dict[ "word" ];

    if ( !p ) {
        p.reset( std::make_shared<std::set<int>>() ); // error
        p.reset( new std::set<int>() );               // fine
    }

    return 0;
}
Last edited on
1
2
p.reset( std::make_shared<std::set<int>>() );
p = std::make_shared<std::set<int>>();
Topic archived. No new replies allowed.