<bdo id='zxWRY'></bdo><ul id='zxWRY'></ul>
    1. <tfoot id='zxWRY'></tfoot>

    2. <legend id='zxWRY'><style id='zxWRY'><dir id='zxWRY'><q id='zxWRY'></q></dir></style></legend>

      <small id='zxWRY'></small><noframes id='zxWRY'>

        <i id='zxWRY'><tr id='zxWRY'><dt id='zxWRY'><q id='zxWRY'><span id='zxWRY'><b id='zxWRY'><form id='zxWRY'><ins id='zxWRY'></ins><ul id='zxWRY'></ul><sub id='zxWRY'></sub></form><legend id='zxWRY'></legend><bdo id='zxWRY'><pre id='zxWRY'><center id='zxWRY'></center></pre></bdo></b><th id='zxWRY'></th></span></q></dt></tr></i><div id='zxWRY'><tfoot id='zxWRY'></tfoot><dl id='zxWRY'><fieldset id='zxWRY'></fieldset></dl></div>
      1. 如何让 std::make_unique 成为我班的朋友

        时间:2023-05-24

          <legend id='2G6JS'><style id='2G6JS'><dir id='2G6JS'><q id='2G6JS'></q></dir></style></legend>
            <tfoot id='2G6JS'></tfoot>

              <i id='2G6JS'><tr id='2G6JS'><dt id='2G6JS'><q id='2G6JS'><span id='2G6JS'><b id='2G6JS'><form id='2G6JS'><ins id='2G6JS'></ins><ul id='2G6JS'></ul><sub id='2G6JS'></sub></form><legend id='2G6JS'></legend><bdo id='2G6JS'><pre id='2G6JS'><center id='2G6JS'></center></pre></bdo></b><th id='2G6JS'></th></span></q></dt></tr></i><div id='2G6JS'><tfoot id='2G6JS'></tfoot><dl id='2G6JS'><fieldset id='2G6JS'></fieldset></dl></div>
              • <bdo id='2G6JS'></bdo><ul id='2G6JS'></ul>

                <small id='2G6JS'></small><noframes id='2G6JS'>

                    <tbody id='2G6JS'></tbody>
                1. 本文介绍了如何让 std::make_unique 成为我班的朋友的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想将 std::make_unique 函数声明为我班级的朋友.原因是我想声明我的构造函数 protected 并提供使用 unique_ptr 创建对象的替代方法.这是一个示例代码:

                  I want to declare std::make_unique function as a friend of my class. The reason is that I want to declare my constructor protected and provide an alternative method of creating the object using unique_ptr. Here is a sample code:

                  #include <memory>
                  
                  template <typename T>
                  class A
                  {
                  public:
                      // Somehow I want to declare make_unique as a friend
                      friend std::unique_ptr<A<T>> std::make_unique<A<T>>();
                  
                  
                      static std::unique_ptr<A> CreateA(T x)
                      {
                          //return std::unique_ptr<A>(new A(x)); // works
                          return std::make_unique<A>(x);         // doesn't work
                      }
                  
                  protected:
                      A(T x) { (void)x; }
                  };
                  
                  int main()
                  {
                      std::unique_ptr<A<int>> a = A<int>::CreateA(5);
                      (void)a;
                      return 0;
                  }
                  

                  现在我收到此错误:

                  Start
                  In file included from prog.cc:1:
                  /usr/local/libcxx-head/include/c++/v1/memory:3152:32: error: calling a protected constructor of class 'A<int>'
                  return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
                                             ^
                  prog.cc:13:21: note: in instantiation of function template specialization 'std::__1::make_unique<A<int>, int &>' requested here
                      return std::make_unique<A>(x);     // doesn't work
                                  ^
                  prog.cc:22:41: note: in instantiation of member function 'A<int>::CreateA' requested here
                  std::unique_ptr<A<int>> a = A<int>::CreateA(5);
                                                      ^
                  prog.cc:17:5: note: declared protected here
                  A(T x) { (void)x; }
                  ^
                  1 error generated.
                  1
                  Finish
                  

                  std::make_unique 声明为我班级朋友的正确方法是什么?

                  What is the correct way to declare std::make_unique as a friend of my class?

                  推荐答案

                  make_unique 完美转发你传递给它的参数;在您的示例中,您将左值 (x) 传递给函数,因此它会将参数类型推导出为 int&.你的 friend 函数声明需要

                  make_unique perfect forwards the arguments you pass to it; in your example you're passing an lvalue (x) to the function, so it'll deduce the argument type as int&. Your friend function declaration needs to be

                  friend std::unique_ptr<A> std::make_unique<A>(T&);
                  

                  同样,如果你要在 CreateAmove(x)friend 声明需要是

                  Similarly, if you were to move(x) within CreateA, the friend declaration would need to be

                  friend std::unique_ptr<A> std::make_unique<A>(T&&);
                  

                  这将使代码编译,但绝不保证它会编译另一种实现,因为就您所知,make_unique 将其参数转发给另一个实际实例化您的类的内部辅助函数,在这种情况下,辅助函数需要是 friend.

                  This will get the code to compile, but is in no way a guarantee that it'll compile on another implementation because for all you know, make_unique forwards its arguments to another internal helper function that actually instantiates your class, in which case the helper would need to be a friend.

                  这篇关于如何让 std::make_unique 成为我班的朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:尝试将字符串文字作为模板参数传递 下一篇:为什么stack&lt;const string&gt;不能在 g++ 中编译

                  相关文章

                  最新文章

                2. <tfoot id='QXm3P'></tfoot>

                  <small id='QXm3P'></small><noframes id='QXm3P'>

                  <legend id='QXm3P'><style id='QXm3P'><dir id='QXm3P'><q id='QXm3P'></q></dir></style></legend>
                  <i id='QXm3P'><tr id='QXm3P'><dt id='QXm3P'><q id='QXm3P'><span id='QXm3P'><b id='QXm3P'><form id='QXm3P'><ins id='QXm3P'></ins><ul id='QXm3P'></ul><sub id='QXm3P'></sub></form><legend id='QXm3P'></legend><bdo id='QXm3P'><pre id='QXm3P'><center id='QXm3P'></center></pre></bdo></b><th id='QXm3P'></th></span></q></dt></tr></i><div id='QXm3P'><tfoot id='QXm3P'></tfoot><dl id='QXm3P'><fieldset id='QXm3P'></fieldset></dl></div>
                      <bdo id='QXm3P'></bdo><ul id='QXm3P'></ul>