简体   繁体   中英

C++ Initializing a unique_ptr array attribute in a class

I wish to use a unique_ptr for my class here (instead of vector for my own reasons). But I'm not sure how to initialize a new [] array with it. Here is what my code looks like

template <typename T>
class kmap
{
private:
  const std::vector<std::string> &data;
  std::unique_ptr<T> table;
public:
  kmap(std::vector<std::string> &kmers);
};

template <typename T>
kmap<T>::kmap(std::vector<std::string> &kmers) : data(kmers)
{
  this->table = std::unique_ptr<T[]>(new T[kmers.size()*2]);
}

Now I can initialize this with std::unique_ptr<T>(new T[kmers.size()*2] ); but I am skeptical that it may only delete the very first element of the array instead of freeing the entire block when the object goes out of scope. Is my skepticism in vain or is it possible to initialize this as an array?

Here is a part of the error message of what I get when initializing this with T = int :

/home/sflash/Documents/misc/kmap.cpp: In instantiation of ‘kmap<T>::kmap(std::vector<std::__cxx11::basic_string<char> >&) [with T = int]’:
/home/sflash/Documents/misc/kmap.cpp:75:23:   required from here
/home/sflash/Documents/misc/kmap.cpp:27:15: error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<int, std::default_delete<int> >’ and ‘std::unique_ptr<int [], std::default_delete<int []> >’)
   27 |   this->table = std::unique_ptr<T[]>(new T[kmers.size()*2]);
      |   ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/10.2.0/memory:83,
                 from /home/sflash/Documents/misc/kmap.cpp:2:
/usr/include/c++/10.2.0/bits/unique_ptr.h:371:19: note: candidate: ‘std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = int; _Dp = std::default_delete<int>]’
  371 |       unique_ptr& operator=(unique_ptr&&) = default;
      |                   ^~~~~~~~
/usr/include/c++/10.2.0/bits/unique_ptr.h:371:29: note:   no known conversion for argument 1 from ‘std::unique_ptr<int [], std::default_delete<int []> >’ to ‘std::unique_ptr<int, std::default_delete<int> >&&’
  371 |       unique_ptr& operator=(unique_ptr&&) = default;
      |                             ^~~~~~~~~~~~
/usr/include/c++/10.2.0/bits/unique_ptr.h:386:2: note: candidate: ‘template<class _Up, class _Ep> typename std::enable_if<std::__and_<std::__and_<std::is_convertible<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::__uniq_ptr_impl<_Tp, _Dp>::pointer>, std::__not_<std::is_array<_Up> > >, std::is_assignable<_T2&, _U2&&> >::value, std::unique_ptr<_Tp, _Dp>&>::type std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Up, _Ep>&&) [with _Up = _Up; _Ep = _Ep; _Tp = int; _Dp = std::default_delete<int>]’
  386 |  operator=(unique_ptr<_Up, _Ep>&& __u) noexcept

How I compiled: g++ -pipe -O2 -std=c++14 "$file" -o exe -lm

unique_ptr has a specialization when declared with an array type:

std::unique_ptr<T[]> table;

In this situation, delete[] will be used to delete the pointer.

As a bonus, this specialization also defines an overloaded operator[] to make it easier to use as an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM