我不熟悉 C++ 中的右值引用,想学习如何在日常生活中使用它们。 我有 2 个关于流行用例的相关问题:将右值引用与 boost::in_place 和 boost::bind 结合使用。
考虑一个类,构造函数将右值引用作为参数:
struct A
: boost::noncopyable
{
A(int&&){}
};
现在让我们尝试为这个类创建 boost 可选变量:
void foo(int&& value)
{
boost::optional<A> opt;
// some code here
opt = boost::in_place(std::forward<int>(value)); //Error!
}
在这样的例子中传递右值 ref 的正确方法是什么。是否有类似 boost::reference_wrapper 的右值引用的解决方案?
另一个常见的用例是将 boost::bind 仿函数对象分配给 boost::function 对象。
void foo(int&&)
{
}
void bar()
{
boost::function<void(int&&)> func;
int x = 0;
func = boost::bind(foo, std::move(x)); // Compilation error (a)
func = boost::bind(foo, _1); // Compilation error too (b)
}
我知道指令(a)在第一次调用后可能会导致 undefined variable 值,但指令(b)甚至没有这样的问题。但是如何正确编写这段代码呢?
最佳答案
boost in_places 本身不支持移动或右值引用。但是,您可以创建一个继承自 boost::in_place_factory_base 的新类,并创建您自己的类。或者你可以使用一个支持 emplace 函数的可选(比如 folly::Optional)。
我不确定我是否推荐这样做,但您可以在 boost 中创建一个通用重载来处理它,如下所示:
#include <boost/optional.hpp>
#include <boost/utility/in_place_factory.hpp>
#include <folly/ApplyTuple.h>
namespace boost
{
template<class ... As>
struct rvalue_in_place : public in_place_factory_base
{
mutable std::tuple<As...> tup_;
rvalue_in_place(As ... as)
: tup_(std::forward<As>(as)...)
{
}
template< class T >
void apply ( void* address ) const
{
auto make = [address](As ... as) { new (address) T(std::forward<As>(as)...); };
folly::applyTuple(make, tup_);
}
};
template <class ... As>
rvalue_in_place<As&&...> in_place(As && ... a)
{
return rvalue_in_place<As&&...>(std::forward<As>(a)...);
}
}
关于c++ - 将右值引用传递给 boost::in_place 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11346323/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我看到这个错误:translationmissing:da.datetime.distance_in_words.about_x_hours我的语言环境文件:http://pastie.org/2944890我的看法:我已将其添加到我的application.rb中:config.i18n.load_path+=Dir[Rails.root.join('my','locales','*.{rb,yml}').to_s]config.i18n.default_locale=:da如果我删除I18配置,帮助程序会处理英语。更新:我在config/enviorments/devolpment
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些