using c++ aggregate initialization in std::make_shared

后端 未结 2 1799
抹茶落季
抹茶落季 2020-12-05 13:58

Per my understanding, the following code constructs an object of type Foo and then moves that object into the memory allocated by std::make_shared<

相关标签:
2条回答
  • 2020-12-05 14:39

    You could create an adapter with a variadic constructor template to forward the arguments, something like:

    template<class T>
    struct aggregate_adapter : public T {
        template<class... Args>
        aggregate_adapter(Args&&... args) : T{ std::forward<Args>(args)... } {}
    };
    

    And then you can do:

    auto foo = std::make_shared<aggregate_adapter<Foo>>("hello", 5, 'c');
    

    Since aggregate_adapter<Foo> and Foo are related, foo is convertible to std::shared_ptr<Foo> as well.

    Caveats


    Unfortunately, the use of forwarding also makes it impossible to brace-init any of the members like std::make_shared<aggregate_adapter<Foo>>({'h','e','l','l','o'}, 5, 'c'); without specifying the type explicitly, but the same restriction applies to make_shared already.

    0 讨论(0)
  • 2020-12-05 14:53

    The inheritance solution feels a bit overkill to me, and adds a bit of readability confusion because the make_shared template is not your type.

    Unaware readers might be confused about this aggregate_adapter part.

    Here is an alternative:

    #include <iostream>
    struct Foo
    {
        std::string s;
        int i;
        char c;
    };
    
    template<typename T, typename... Args>
    std::shared_ptr<T> MakeAggregateShared(Args&&... args)
    {
        return std::make_shared<T>(T{ std::forward<Args>(args)... });
    }
    
    int main(int argc, char* argv[])
    {
        auto foo = MakeAggregateShared<Foo>("hello", 5, 'c');
    }
    
    0 讨论(0)
提交回复
热议问题