2

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?

6
  • 1
    An std::array is a fixed size array. You probably want an std::vector. Read on both options and decide for yourself. Commented Jul 6, 2020 at 8:23
  • 2
    I've never really seen the use-case for std::unique_ptr<T[]> when we have std::vector<T>. If ownership semantics is wanted then std::unique_ptr<std::vector<T>> is probably better (and a good use-case for pointers to containers, which is generally not common). Commented Jul 6, 2020 at 8:25
  • 1
    where you declare magnitudes? Commented Jul 6, 2020 at 8:28
  • 2
    @Someprogrammerdude It can be useful when your legacy code hands you a pointer that you get to own and you want to avoid copying. I don't see a case for ever creating one "from scratch", though. Commented Jul 6, 2020 at 8:57
  • 1
    Your error is not coming from standard c++ and might be incorrect. It looks like it wants you to use only vectors in your code. Commented Jul 6, 2020 at 9:36

2 Answers 2

2

How can I fix this problem

Switch std::unique_ptr<double[]> to std::vector<double>.

Is there any way to initialize such an array without looping through each element?

my_class::my_class(std::size_t new_size, double new_values) 
: my_list(new_size, new_values) 
{}
Sign up to request clarification or add additional context in comments.

Comments

1

There are two ways to fix this problem.

  1. keep your std::unique_ptr<double[]>, and you do not need to alter the rest of your code, but you need to replace these two lines
    auto size = static_cast<const size_t>(new_size);
    my_list = std::make_unique<double[]>(size);          // <-- Error

with this:

    my_list = std::make_unique<double[]>(new_size);  // allocates new_size of doubles
  1. As Caleth pointed out, you can change the type of data member my_list from std::unique_ptr<double[]> to std::vector<double>, but then you need to change all the placed that access my_list.

How can I fix this problem and also is there any way to initialize such an array without looping through each element?

If you keep my_list as unique_ptr, then you can use std::fill_n(my_list.get(), new_size, new_values) to initialize your array.

3 Comments

I am using solution 1 but I end up with a different error message "no matching function for call to make_unique"
Sorry, syntax error, it should be std::make_unique<double[]>(new_size);
solution 1 is not really required. What he's doing is redundant, but valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.