jjzjj

fast-forward

全部标签

c++ - 组合 std::forward、std::move 和 volatile 时的意外返回类型

代码ongcc.godbolt.org.我创建了一个简单的类型特征来删除右值引用:templatestructremove_rvalue_reference{usingtype=T;};templatestructremove_rvalue_reference{usingtype=T;};templateusingremove_rvalue_reference_t=typenameremove_rvalue_reference::type;我用它来实现一个copy_if_rvalue(x)函数,其返回类型取决于传递的参数:templateconstexprautocopy_if_rva

c++ - std::forward_list::remove_if 谓词的要求

考虑这段代码:structT{boolstatus;UsefulDatadata;};std::forward_listlst;lst.remove_if([](T&x)->bool{returnx.status=!x.status;});即一次性切换状态和删除非事件元素。根据cppreference上面的代码似乎是未定义的行为(强调我的):templatevoidremove_if(UnaryPredicatep);p-unarypredicatewhichreturnstrueiftheelementshouldberemoved.Thesignatureofthepredicat

c++ - 如何使用 std::forward_list 在恒定时间内进行范围拼接?

我想拼接范围[first,last],包括两个端点。我有元素beforefirst和last的迭代器。我可以使用splice_after()来完成,但只能在线性时间内完成。我相信这个拼接可以在恒定时间内完成。我如何使用std::forward_list完成它?如果问题不清楚,这里是显示我的问题的示例代码:LiveWorkSpace上的代码#include#include#include#includeusingnamespacestd;intmain(){forward_listtrg{'a','b','c'};forward_listsrc{'1','2','3','4'};auto

c++ - std::move 与 std::forward

这似乎是已经提出的最相关的问题。Whatsthedifferencebetweenstd::moveandstd::forward但是每个答案都是不同的,适用的和说的东西也略有不同。所以我很困惑。我有以下情况。将项目复制到容器中Copy项是C++03,所以我非常理解。将项目构建到容器中Constructitemintocontainer我相信使用完美转发正确地通过两个函数将参数转发到emplaceBackInternal()中T的构造函数(如果我错了请指出)。将元素移入容器我的问题似乎是理解如何将元素移入容器。代码:templateclassContainer{std::size_tl

c++ - 编译器如何知道必须调用 std::forward 函数的哪个重载?

以下签名被声明为std::forward重载:templateT&&forward(typenameremove_reference::type&arg)noexcept;templateT&&forward(typenameremove_reference::type&&arg)noexcept;现在,考虑以下模板函数:templateT&&foo_as_always(T&&t){returnstd::forward(t);}如果我写:inti=0;foo_as_always(i);然后这就是编译器如何使用T=int&实例化foo_as_always:int&foo_as_alway

c++ - VC++ 使用 fp :fast causes wrong (not just inaccurate) results - is this a compiler bug?

我已经安装了最新的VS2017更新(15.4.4),但在编译我们的项目时,单元测试开始失败。在使用优化(/O2)和浮点快速模型(/fp:fast)时,问题似乎发生在某些情况下。以前的编译器(VS2017update15.2)没有出现这个问题。这是一个示例程序:#includeconstfloatFACTOR=0.01745329251994329576923690768489f;unsignedlonglonghoursToMicrosecs(inthours){returnhours*3600*1000000LL;}floatdegToRad(floatdeg){returndeg*

c++ - 标准 :forward inside a template class

templateclassBlockingQueue{std::queuecontainer_;templatevoidpush(U&&value){static_assert(std::is_same::type>::value,"Can'tcallpushwithoutthesameparameterastemplateparameter'sclass");container_.push(std::forward(value));}};我希望BlockingQueue::push方法能够处理T类型对象的右值和左值引用,以将其转发到std::queue::push正确的版本。是像上面

c++ - 为什么要使用 std::forward?

在下面的代码中,为什么要在传递参数时使用std::forward?classTest{public:Test(){std::coutvoidpass(Arg&&arg){//usearg..return;}templatevoidpass(Arg&&arg,Args&&...args){//usearg...returnpass(args...);//whyshouldIusestd::forward(args)...?}intmain(intargc,char**argv){pass(std::move(Test()));return0;}带有或不带有std::forward的代码不

c++ - std::forward_list - 如何在末尾插入元素

这个问题在这里已经有了答案:std::forward_listandstd::forward_list::push_back(5个答案)关闭9年前。forward_list是一个单链表(不同于标准的列表容器)。list具有在前面和后面插入的功能,但forward_list没有在后面插入元素的功能(类似于push_back)。为什么不能在列表的后面插入一个元素?

c++ - 模板 friend 类 : Forward Declaration or. ..?

假设我有一个模板类,我试图将其声明为友元类。我应该转发声明类还是给它自己的模板?例子:templateclassSLinkedList;templateclassSNode{private:Eelem;SNode*next;friendclassSLinkedList;};或者templateclassSNode{private:Eelem;SNode*next;templatefriendclassSLinkedList;}; 最佳答案 您的第一种方法可能就是您想要的。它将使SLinkedListSNode的friend,并且所有