jjzjj

forward-compatibility

全部标签

c++ - 在 C++ 中将惰性生成器实现为 forward_iterator

MyGenerator代表一个(可能)有限的整数序列,计算起来很昂贵。所以我不想预先生成它们并将它们放入容器中。structMyGenerator{boolHasNext();intNext();}全部打印:MyGeneratorgenerator;while(generator.HasNext()){std::cout如何实现类似的遵循forward_iterator协议(protocol)的生成器?boost::function_input_iterator接近,但我不知道预先元素的数量。 最佳答案 首先,查看boost::fu

c++ std::forward 在容器上调用 operator[]

《EffectivemodernC++》一书中第3条写了这样一段代码:templatedecltype(auto)authAndAccess(Container&&c,Indexi){authenticateUser();returnstd::forward(c)[i];}我不明白你为什么调用std::forward?如果c是右值引用,那么在右值而不是左值上调用operator[]会发生什么变化?对我来说c[i]应该足够了。PS:当变量是函数的参数时,我理解std::forward的目的是:templatestd::unique_ptrmake_unique(Ts&&...params

c++ - static_cast<T&&>(t) 编译速度比 std::forward<T>(t) 快?

最近,我在这里阅读了range-v3的提交评论:https://github.com/ericniebler/range-v3/commit/a4829172c0d6c43687ba213c54f430202efd7497提交消息说,marginallyimprovecompiletimesbyreplacingstd::forwardwithstatic_cast我知道std::forward(t)返回static_cast(t),按照标准。我也知道有时static_cast(t)当T&&t时会正常工作是通过引用折叠规则的通用引用(或转发引用)。我感兴趣的是提交消息说static_c

c++ - 为什么 std::forward 将左值和右值转换为右值引用?

我想我对std::forward感到困惑.我的函数使用std::forward如下,但为了便于解释,它进行了很多简化和修改。//Thisisanexamplecodetoexplainmyquestionsimply.templatevoidadd(Element&&element){staticstd::vectorvec;vec.push_back(std::forward(element));}我用上面的函数尝试了两种情况;Case1左值参数和Case2右值参数。案例1:左值参数autosome_class=SomeClass();add(some_class);案例2:右值参数

c++ - std::forward of rvalue ref to lambda?

考虑以下两个片段:附件A:templateintperform_calc(CalcFuncT&&calcfunc){precalc();intconstcalc=calcfunc();postcalc();returncalc;}intmain(){perform_calc([]{return5*foobar_x()+3;});//toFutureperform_calc([]{return5*foobar_y()-9;});//toPast}图表B:templateintperform_calc(CalcFuncT&&calcfunc){precalc();intconstcalc=

c++ - forward_list::splice_after( const_iterator pos, forward_list& other, const_iterator i ) 功能

我正在阅读有关此功能工作方式的不同解释。cplusplus.com说这个函数应该“直接在i之后移动元素”。然而cppreference.com表示它拼接元素ATi。MSvisualstudio同意cplusplus.com。但是,实际上正确的行为是什么?我倾向于认为“在i之后”移动更合乎逻辑(&不需要N时间来找到前面的节点)。(PS:没有forward-list标签?) 最佳答案 23.3.4.6voidsplice_after(const_iteratorposition,forward_list&x,const_iterator

c++ - g++ 认为我的类声明是 "forward declaration"

这个问题在这里已经有了答案:C++staticpolymorphism(CRTP)andusingtypedefsfromderivedclasses(5个答案)关闭3年前。精简到最低限度,这是我要编译的代码:templateclassB{protected:std::vectorv;public:templatevoidadd(Args...args){this->v.emplace_back(std::forward(args)...);}typenameT::Iget(inti){returnthis->v[i];}};classD:publicB{public:typedefs

c++ - G++ 4.5.0 中的 std::forward_as_tuple

我迫切需要函数std::forward_as_tuple,但仅限于使用GCC4.5.0(我知道这对自己来说是一个糟糕的情况,但它会为我解决很多问题,所以请将尖刻的言论保持在最低限度)。header似乎不包含函数(它应该包含),所以我的问题是:它是否隐藏在其他标题中?(这以前发生过,但很难确定。)是否可以推出自己的实现方案?即:在GCC4.5.0中实现的c++11部分是否可以实现?如果有人真正知道如何做到这一点,将获得奖励。 最佳答案 实现很简单:template/*constexpr*/tupleforward_as_tuple(E

c++ - 带按值传递参数的 std::forward

我在阅读有关封装多态性的文章时遇到了一段这样的代码:templatestructModel:Concept{Model(Timpl):mImpl(std::forward(impl)){}virtualConcept*clone()constoverride{returnnewModel(mImpl)}virtualvoidoperator(constLogMessage::Meta&meta,conststd::string&message)override{mImpl(meta,message);}TmImpl;};在模型构造函数中转发impl有什么意义?如果按值传递参数,转发参数

c++ - 为什么转发引用需要 std::forward

这个问题在这里已经有了答案:Whatarethemainpurposesofstd::forwardandwhichproblemsdoesitsolve?(7个答案)关闭5年前。在这样的函数模板中templatevoidfoo(T&&x){bar(std::forward(x));}不是xfoo中的右值引用,如果foo用右值引用调用?如果使用左值引用调用foo,则无论如何都不需要强制转换,因为x也将是foo内部的左值引用.还有T将被推导为左值引用类型,因此std::forward不会改变x的类型.我使用boost::typeindex进行了测试使用和不使用std::forward时,