假设命名空间 std贯穿始终。
C++14 委员会草案 N3690 定义了 std::make_unique因此:
[n3690: 20.9.1.4]:unique_ptrcreation [unique.ptr.create]
template <class T, class... Args> unique_ptr<T> make_unique(Args&&... args);1 Remarks: This function shall not participate in overload resolution unless
Tis not an array.
2 Returns:unique_ptr<T>(new T(std::forward<Args>(args)...)).
template <class T> unique_ptr<T> make_unique(size_t n);3 Remarks: This function shall not participate in overload resolution unless
Tis an array of unknown bound.
4 Returns:unique_ptr<T>(new typename remove_extent<T>::type[n]()).
template <class T, class... Args> unspecified make_unique(Args&&...) = delete;5 Remarks: This function shall not participate in overload resolution unless
Tis an array of known bound.
现在,这对我来说似乎是一团糟,我认为它需要更多的阐述。但是,除了这篇社论评论,我相信我已经解码了每个变体的含义:
template <class T, class... Args> unique_ptr<T> make_unique(Args&&... args);
你的沼泽标准make_unique对于非数组类型。据推测,“备注”表明某种形式的静态断言或 SFINAE 技巧是为了防止模板在 T 时被成功实例化。是数组类型。
在高层次上,将其视为等同于 T* ptr = new T(args); 的智能指针.
template <class T> unique_ptr<T> make_unique(size_t n);
数组类型的变体。创建一个动态分配的数组 n × Ts , 并将其包裹在 unique_ptr<T[]> 中返回.
在高层次上,将其视为等同于 T* ptr = new T[n]; 的智能指针.
template <class T, class... Args> unspecified make_unique(Args&&...)
不允许。 “未指定”可能是 unique_ptr<T[N]> .
否则智能指针相当于无效的T[N]* ptr = new (keep_the_dimension_please) (the_dimension_is_constexpr) T[N]; .
首先,我是对的吗?如果是这样,第三个功能是怎么回事?
如果它禁止程序员在为每个元素提供构造函数参数时尝试动态分配数组(就像 new int[5](args) 是不可能的),那么第一个函数不能为数组类型实例化,不是吗?
如果它是为了防止在语言中添加类似 T[N]* ptr = new T[N] 的结构(其中 N 是一些 constexpr )那么,为什么? unique_ptr<T[N]> 不是完全有可能吗?存在包装一个动态分配的 block N × T秒?如果委员会已经竭尽全力禁止使用 make_unique 创建它,这会是一件坏事吗? ?
为什么是make_unique<T[N]>不允许?
最佳答案
T[N]As of N3485,
unique_ptrdoesn't provide a partial specialization forT[N]. However, users will be strongly tempted to writemake_unique<T[N]>(). This is a no-win scenario. Returningunique_ptr<T[N]>would select the primary template for single objects, which is bizarre. Returningunique_ptr<T[]>would be an exception to the otherwise ironclad rule thatmake_unique<something>()returnsunique_ptr<something>. Therefore, this proposal makesT[N]ill-formed here, allowing implementations to emit helpfulstatic_assertmessages.
该提案的作者 Stephan T. Lavavej 在 this video on Core C++ 中说明了这种情况。 (由 chris 提供),从 1:01:10 分钟开始(或多或少)。
关于c++ - 为什么不允许使用 `make_unique<T[N]>`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20313322/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我正在学习如何在我的Ruby代码中使用Module.prepend而不是alias_method_chain,我注意到有些人使用send调用它(example):ActionView::TemplateRenderer.send(:prepend,ActionViewTemplateRendererWithCurrentTemplate)而其他人直接调用它(example):ActionView::TemplateRenderer.prepend(ActionViewTemplateRendererWithCurrentTemplate)而且,虽然我还没有看到任何人使用这种风格,但我从