【问题标题】:Build error in Visual Studio 2013 with `using std::unique_ptr<T>::unique_ptr`使用 std::unique_ptr<T>::unique_ptr 在 Visual Studio 2013 中构建错误
【发布时间】:2020-07-06 22:24:10
【问题描述】:

我正在尝试更新一些源代码以使其与 Visual Studio 2013 兼容。

在某些时候,我在以下模板上出现错误:

// Means of generating a key for searching STL collections of std::unique_ptr
// that avoids the side effect of deleting the pointer.
template <class T>
class FakeUniquePtr : public std::unique_ptr<T> {
 public:
  using std::unique_ptr<T>::unique_ptr;
  ~FakeUniquePtr() { std::unique_ptr<T>::release(); }
};

我收到以下错误:

error C2886: 'unique_ptr<_Ty,std::default_delete<_Ty>>' : symbol cannot be used in a member using-declaration

我想知道如何调整此代码以使其与 Visual Studio 2013 兼容,以及该代码的含义是什么。如何更新它以使代码与 VS2013 兼容?

【问题讨论】:

  • 根据devblogs.microsoft.com/cppblog/… VS2013 不支持此功能(继承构造函数)。您很可能必须将其替换为您自己的调用 unique_ptr 构造函数的构造函数
  • 所以基本上我需要编写FakeUniquePtr的构造函数来调用std::unique_ptr的相应构造函数?
  • 据我所知,是的。(根据提案:open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2540.htm,这是该功能应该解决的问题)。也许其他人有更好的主意
  • @UnholySheep 你应该这样回答。顺便说一句,它与 VS2019 一起编译(如预期的那样)。
  • 是的,它可以构建,但我需要对其进行调整,因为我需要在使用 VS2013 创建的旧项目中使用此代码,并且无法更新编译器。

标签: c++ visual-studio visual-studio-2013 using


【解决方案1】:

根据这篇微软博客文章:https://devblogs.microsoft.com/cppblog/c1114-core-language-features-in-vs-2013-and-the-nov-2013-ctp/这个功能,称为“继承构造函数”,在 Visual Studio 2013 的常规版本中不可用(文章提到了 2013 年 11 月的 CTP 版本,它确实支持它) 如果没有此功能,您将不得不编写调用等效的 std::unique_ptr 构造函数的构造函数(这将使类更加冗长),例如:

template <class T>
class FakeUniquePtr : public std::unique_ptr<T> {
 private:
  // typedef to minimize writing std::unique_ptr<T>
  typedef std::unique_ptr<T> base; 
 public:
  FakeUniquePtr() : base(){}
  // repeat for all other constructors
  ~FakeUniquePtr() { base::release(); }
};

【讨论】:

    猜你喜欢
    • 2015-10-11
    • 1970-01-01
    • 2020-04-17
    • 2019-11-07
    • 2013-10-18
    • 2012-05-12
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多