I am writing a constructor for a class containing the following:
// my_class.hpp
private:
std::unique_ptr<double[]> my_list;
and I am facing some problems when trying to initialize my_list
my_class::my_class(const int new_size, const double new_values){
// creating an array with new_values as initial value.
auto size = static_cast<const size_t>(new_size);
my_list = std::make_unique<double[]>(size); // <-- Error
for (size_t i = 0; i < size; i++){
my_list[i] = new_values;
}
}
Error Message: "do not declare C-style arrays, use std::array<> instead [modernize-avoid-c-arrays,-warnings-as-errors]"
I have tried the following:
my_list = std::make_unique<std::array<double>>(size); // <-- Error
// Error Message: "too few template arguments for class template 'array' [clang-diagnostic-error]"
and
my_list = std::make_unique<std::array<double,size>>(size); // <-- Error
// Error Message: "non-type template argument is not a constant expression [clang-diagnostic-error]"
How can I fix this problem and also is there any way to initialize such an array without looping through each element?
std::unique_ptr<T[]>when we havestd::vector<T>. If ownership semantics is wanted thenstd::unique_ptr<std::vector<T>>is probably better (and a good use-case for pointers to containers, which is generally not common).magnitudes?vectors in your code.