C++ 分配数组时是否可以将参数传递给std::make_unique()?

C++ 分配数组时是否可以将参数传递给std::make_unique()?,c++,c++11,c++14,C++,C++11,C++14,在下面的代码中,当使用std::make_unique()分配demo[]数组时,是否有任何方法可以将参数传递给demo构造函数 class demo{ public: int info; demo():info(-99){} // default value demo(int info): info(info){} }; int main(){ // ok below code creates default constructor, totally fine,

在下面的代码中,当使用
std::make_unique()
分配
demo[]
数组时,是否有任何方法可以将参数传递给
demo
构造函数

class demo{
public:
    int info;
    demo():info(-99){} // default value
    demo(int info): info(info){}
};
int main(){
    // ok below code creates default constructor, totally fine, no problem
    std::unique_ptr<demo> pt1 = std::make_unique<demo>();

    // and this line creates argument constructor, totally fine, no problem
    std::unique_ptr<demo> pt2 = std::make_unique<demo>(1800);

    // But now, look at this below line

    // it creates 5 object of demo class with default constructor

    std::unique_ptr<demo[]> pt3 = std::make_unique<demo[]>(5);

    // but I need here to pass second constructor argument, something like this : -

    //std::unique_ptr<demo[]> pt3 = std::make_unique<demo[]>(5, 200);
    return 0;
}
类演示{
公众:
国际信息;
demo():info(-99){}//默认值
演示(int-info):info(info){}
};
int main(){
//下面的代码创建默认构造函数,非常好,没有问题
std::unique_ptr pt1=std::make_unique();
//这一行创建了参数构造函数,很好,没问题
std::unique_ptr pt2=std::make_unique(1800);
//但现在,看看下面这行
//它使用默认构造函数创建演示类的5个对象
std::unique_ptr pt3=std::make_unique(5);
//但我需要在这里传递第二个构造函数参数,类似这样:-
//std::unique_ptr pt3=std::make_unique(5200);
返回0;
}
std::make_unique()
不支持将参数传递给数组元素的构造函数。它总是只调用默认构造函数。您必须手动构建阵列,例如:

std::unique_ptr pt3(新演示[5]{200200});
如果要创建大量元素,这显然是没有用的。如果您不介意在构建它们之后重新初始化它们,您可以这样做:

std::unique_ptr pt3=std::make_unique(5);
std::fill_n(pt3.get(),5200);
否则,只需使用
std::vector

std::vector pt3(5200);

改用
std::vector
。除了标题之外,我期待的是
std::unique\u ptr[]
,而不是
std::unique\u ptr