make_unique and perfect forwarding

后端 未结 6 1862
南方客
南方客 2020-11-22 10:24

Why is there no std::make_unique function template in the standard C++11 library? I find

std::unique_ptr p(new SomeUs         


        
6条回答
  •  半阙折子戏
    2020-11-22 11:04

    Inspired by the implementation by Stephan T. Lavavej, I thought it might be nice to have a make_unique that supported array extents, it's on github and I'd love to get comments on it. It allows you to do this:

    // create unique_ptr to an array of 100 integers
    auto a = make_unique();
    
    // create a unique_ptr to an array of 100 integers and
    // set the first three elements to 1,2,3
    auto b = make_unique(1,2,3); 
    

提交回复
热议问题