Shared Pointer Creation: new vs make_shared

Question 6 / 17 Correct so far: 0 (0 answered)

Snippet A

New Shared Ptr

std::shared_ptr<Widget> createWidget(int id, double value, const std::string& name) {
    return std::shared_ptr<Widget>(new Widget(id, value, name));
}

auto ptr = createWidget(WIDGET_ID, WIDGET_VALUE, WIDGET_NAME);
Snippet B

Make Shared Ptr

std::shared_ptr<Widget> createWidget(int id, double value, const std::string& name) {
    return std::make_shared<Widget>(id, value, name);
}

auto ptr = createWidget(WIDGET_ID, WIDGET_VALUE, WIDGET_NAME);
Shared test data (shared-setup)
struct Widget {
    int id;
    double value;
    std::string name;

    Widget(int id, double value, std::string name)
        : id(id), value(value), name(std::move(name)) {}
};

const int         WIDGET_ID    = 42;
const double      WIDGET_VALUE = 3.14;
const std::string WIDGET_NAME  = "benchmark-widget";

Which snippet is faster?

Select the correct answer(s)