简体   繁体   中英

std::auto_ptr compiles in my template class but not std::unique_ptr

I started a template class that is supposed to manage a fixed length deque. I was looking to add a function that would return the data transformed into a vector. Because I cannot be sure this will be compiled with good NRVO (named return value optimization) and the data could in theory be quite large I decided to wrap the return in a unique_ptr (to avoid a massive call to copy constructors at the end). Oddly enough this did not compile:

 In file included from FixedDeque.cpp:8:
 FixedDeque.h:26: error: ISO C++ forbids declaration of 'unique_ptr' with no type
 FixedDeque.h:26: error: invalid use of '::'
 FixedDeque.h:26: error: expected ';' before '<' token
 FixedDeque.cpp:27: error: expected constructor, destructor, or type conversion before '<' token

I am on OS X snow leopard using g++ as the compiler in NetBeans.

However when I change exactly the same code to use auto_ptr the whole thing compiles. Is there an issue with unique_ptr and templates ? fyi, I did make sure there is a space between contiguous '<' and '>' in both cases (to avoid interpretation of pipe symbols).

Here is my code, I would really appreciate it if someone can shed light on the issue:

Header:

#include "deque"
#include "vector"
#include "memory"
template<typename T> class FixedDeque {
public:
  FixedDeque(int size);
  FixedDeque(const FixedDeque<T>& orig);
  virtual ~FixedDeque();
  // adding an auto_ptr/unique_ptr because not sure the C++ NRVO is applied in the compiler
  std::unique_ptr< std::vector<T> > getVectorCopy() const;
private:
  std::deque<T> _deq;
  int _maxSize;
};


#include "FixedDeque.h"
template<typename T> FixedDeque<T>::FixedDeque(int size)  : _deq(size), _maxSize(size)
{ }
template<typename T> FixedDeque<T>::FixedDeque(const FixedDeque<T>& orig) : _deq(orig._deq) {}
template<typename T> FixedDeque<T>::~FixedDeque() {}  
template<typename T> std::unique_ptr< std::vector<T> > FixedDeque<T>::getVectorCopy() const
{
    std::vector<T>* apVector = new std::vector<T>();
    return( std::unique_ptr< std::vector<T> >(apVector) );
}  

Again, simply replacing unique_ptr with auto_ptr does make the whole thing compile. I know my implementation returns a pointer to an empty vector. I just wanted to focus on what might be wrong with my usage of unique_ptr versus auto_ptr.

Thank you!

Your code is mostly correct, except that you can't compile a class template. To make it work, you have to declare and implement the class template inside the header file, #include the header file and instantiate the class template on your program. See the example below:

// This is FixedDeque.hpp
#include <iostream>
#include <deque>
#include <vector>
#include <memory>

template<typename T> class FixedDeque {
public:
  FixedDeque(int size);
  FixedDeque(const FixedDeque<T>& orig);
  virtual ~FixedDeque();
  // adding an auto_ptr/unique_ptr because not sure the C++ NRVO is applied in the compiler
  std::unique_ptr< std::vector<T> > getVectorCopy() const;
private:
  std::deque<T> _deq;
  int _maxSize;
};

template<typename T> FixedDeque<T>::FixedDeque(int size)  : _deq(size), _maxSize(size)
{ }
template<typename T> FixedDeque<T>::FixedDeque(const FixedDeque<T>& orig) : _deq(orig._deq) {}
template<typename T> FixedDeque<T>::~FixedDeque() {}  
template<typename T> std::unique_ptr< std::vector<T> > FixedDeque<T>::getVectorCopy() const
{
    std::vector<T>* apVector = new std::vector<T>();
    return( std::unique_ptr< std::vector<T> >(apVector) );
}  

// This is FixedDeque_Test.cpp
int main() {
    FixedDeque<int> fdeque(10);
    std::unique_ptr<std::vector<int>> vec = fdeque.getVectorCopy();
    std::cout << vec->size() << std::endl;
    return 0;
}

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