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

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

  1. <legend id='hZ0xn'><style id='hZ0xn'><dir id='hZ0xn'><q id='hZ0xn'></q></dir></style></legend>
    • <bdo id='hZ0xn'></bdo><ul id='hZ0xn'></ul>

      有没有办法在 VS2012 中编写 make_unique() ?

      时间:2023-05-24

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

        • <small id='h6676'></small><noframes id='h6676'>

          1. <tfoot id='h6676'></tfoot>
              <tbody id='h6676'></tbody>
          2. <legend id='h6676'><style id='h6676'><dir id='h6676'><q id='h6676'></q></dir></style></legend>

                本文介绍了有没有办法在 VS2012 中编写 make_unique() ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                Herb Sutter 提出了一个简单的 make_unique() 实现:http://herbsutter.com/gotw/_102/

                Herb Sutter propose a simple implementation of make_unique() there: http://herbsutter.com/gotw/_102/

                这是:

                template<typename T, typename ...Args>
                std::unique_ptr<T> make_unique( Args&& ...args )
                {
                    return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
                }
                

                我的问题是可变参数模板还不是 VS2012 的一部分,所以我不能按原样使用此代码.

                My problem is that variadic templates are not yet part of VS2012, so I can't use this code as is.

                在 VS2012 中是否有一种可维护的方法来编写它,而不涉及使用不同的 args 计数复制粘贴相同的函数?

                推荐答案

                您可以使用 Boost.Preprocessor 来生成不同的参数计数,但我真的没有看到这样做的好处.简单地做一次繁重的工作,把它塞进一个标题中就可以了.您可以节省自己的编译时间并拥有 make_unique.

                You could use Boost.Preprocessor to generate the different parameter counts, but I really don't see the advantage of that. Simply do the grunt job once, stuff it in a header and be done. You're saving yourself compile time and have your make_unique.

                这里是我的 make_unique.h 标头的复制粘贴模拟最多 5 个参数的可变参数模板.

                Here's a copy-paste of my make_unique.h header that simulates variadic templates for up to 5 arguments.

                由于 OP 似乎不喜欢复制粘贴工作,这里是生成上述内容的 Boost.Preprocessor 代码:

                Since OP seems to not like copy-paste work, here's the Boost.Preprocessor code to generate the above:

                首先,创建一个包含多次模板头的主头(Boost.Preprocessor 迭代代码公然从这个答案中窃取)):

                First, make a main header that includes the template header multiple times (Boost.Preprocessor iteration code blatantly stolen from this answer):

                // make_unique.h
                #include <memory>
                #include <utility>
                #include <boost/preprocessor.hpp>
                
                #ifndef MAKE_UNIQUE_NUM_ARGS
                // allow this to be changed to a higher number if needed,
                // ten is a good default number
                #define MAKE_UNIQUE_NUM_ARGS 10
                #endif
                
                #if MAKE_UNIQUE_NUM_ARGS < 0
                // but don't be stupid with it
                #error Invalid MAKE_UNIQUE_NUM_ARGS value.
                #endif
                
                /* optional, see above for premade version
                // include premade functions, to avoid the costly iteration
                #include "detail/blah_premade.hpp
                
                // generate classes if needed
                #if MAKE_UNIQUE_NUM_ARGS > MAKE_UNIQUE_NUM_PREMADE
                */
                #define BOOST_PP_ITERATION_LIMITS (0, MAKE_UNIQUE_NUM_ARGS)
                #define BOOST_PP_FILENAME_1 "make_unique_template.h"
                #include BOOST_PP_ITERATE()
                //#endif
                

                现在制作一个模板标题,根据MAKE_UNIQUE_NUM_ARGS的值一次又一次地包含并以不同的方式扩展:

                And now make a template header that gets included again and again and expands differently depending on the value of MAKE_UNIQUE_NUM_ARGS:

                // make_unique_template.h
                // note: no include guard
                
                #define N BOOST_PP_ITERATION()    
                
                #define MAKE_UNIQUE_TEMPLATE_PARMS 
                  BOOST_PP_ENUM_PARAMS(N, typename A)
                
                #define MAKE_UNIQUE_FUNCTION_PARM(J,I,D) 
                  BOOST_PP_CAT(A,I)&& BOOST_PP_CAT(a,I)
                
                #define MAKE_UNIQUE_FUNCTION_PARMS 
                  BOOST_PP_ENUM(N, MAKE_UNIQUE_FUNCTION_PARM, BOOST_PP_EMPTY)
                
                #define MAKE_UNIQUE_ARG(J,I,D) 
                  std::forward<BOOST_PP_CAT(A,I)>(BOOST_PP_CAT(a,I))
                
                #define MAKE_UNIQUE_ARGS 
                  BOOST_PP_ENUM(N, MAKE_UNIQUE_ARG, BOOST_PP_EMPTY)
                
                template<class T BOOST_PP_COMMA_IF(N) MAKE_UNIQUE_TEMPLATE_PARMS>
                std::unique_ptr<T> make_unique(MAKE_UNIQUE_FUNCTION_PARMS){
                  return std::unique_ptr<T>(new T(MAKE_UNIQUE_ARGS));
                }
                
                // clean up
                #undef MAKE_UNIQUE_TEMPLATE_PARMS
                #undef MAKE_UNIQUE_FUNCTION_PARM
                #undef MAKE_UNIQUE_FUNCTION_PARMS
                #undef MAKE_UNIQUE_ARG
                #undef MAKE_UNIQUE_ARGS
                #undef N
                

                这篇关于有没有办法在 VS2012 中编写 make_unique() ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:带有常量引用的 std::remove_const 下一篇:正在铸造 std::pair&lt;T1, T2&gt;常量&amp;到

                相关文章

                最新文章

                1. <tfoot id='h4tUB'></tfoot>

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

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