c++ - std::auto_ptr 在我的模板类中编译但不是 std::unique_ptr

标签 c++ templates c++11 unique-ptr auto-ptr

我启动了一个模板类,它应该管理一个固定长度的双端队列。我想添加一个函数来返回转换为 vector 的数据。因为我不确定这是否会使用良好的 NRVO(命名返回值优化)进行编译,并且理论上数据可能非常大,所以我决定将返回值包装在 unique_ptr 中(以避免最后大量调用复制构造函数)。奇怪的是,这没有编译:

 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

我在 OS X snow leopard 上使用 g++ 作为 NetBeans 中的编译器。

然而,当我更改完全相同的代码以使用 auto_ptr 时,整个过程都会编译。 unique_ptr 和 templates 有问题吗?仅供引用,我确实确保在这两种情况下连续的“<”和“>”之间有一个空格(以避免解释管道符号)。

这是我的代码,如果有人能阐明这个问题,我将不胜感激:

标题:

#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) );
}  

同样,只需将 unique_ptr 替换为 auto_ptr 即可编译整个程序。我知道我的实现返回一个指向空 vector 的指针。我只想关注我使用 unique_ptr 与 auto_ptr 可能有什么问题。

谢谢!

最佳答案

除了不能编译类模板之外,您的代码大部分都是正确的。为了让它工作,你必须在头文件中声明和实现类模板,#include 头文件并在你的程序中实例化类模板。请参见下面的示例:

// 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;
}

关于c++ - std::auto_ptr 在我的模板类中编译但不是 std::unique_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19121661/

相关文章:

c++ - Boost分离集

c++ - 数组中0和 '0'的区别

C++\Win32 有关所有驱动器的信息 : hard drives, USB 驱动器、磁盘驱动器、软盘驱动器等

ruby - 另一个模板变量中的 Puppet 模板变量

c++ - 专门用于整数值集的模板

c++ - 不可复制类型的 std::initializer_list 替代品

c++ - 转换指针引用的字符串

javascript - 如何在 Odoo 9 中执行 JS 文件中的函数?

c++ - 哪个容器使用 map 或 set 或者其他?

c++11 - 用于weak_ptr 的static_pointer_cast