草庐IT

test_forward_wrapper

全部标签

c++ - 如何正确 "perfect forward"getter 函数?

我正在为C++14创建一个JSON库,并且尽可能使用move语义。我的Value类有几个setter和getter,它们总是尽可能地尝试move:templatevoidsetObj(T&&x){type=Obj;hObj.init(forward(x));}templatevoidsetArr(T&&x){type=Arr;hArr.init(forward(x));}templatevoidsetStr(T&&x){type=Str;hStr.init(forward(x));}auto&getObj()&noexcept{assert(is());returnhObj;}auto

c++ - 如何正确 "perfect forward"getter 函数?

我正在为C++14创建一个JSON库,并且尽可能使用move语义。我的Value类有几个setter和getter,它们总是尽可能地尝试move:templatevoidsetObj(T&&x){type=Obj;hObj.init(forward(x));}templatevoidsetArr(T&&x){type=Arr;hArr.init(forward(x));}templatevoidsetStr(T&&x){type=Str;hStr.init(forward(x));}auto&getObj()&noexcept{assert(is());returnhObj;}auto

c++ - 为什么 std::forward 有两个重载?

鉴于以下引用折叠规则T&&-->T&T&&&-->T&T&&&-->T&T&&&&-->T&&第三条和第四条规则意味着T(refqualifer)&&是恒等变换,即T&停留在T&和T&&停留在T&&.为什么std::forward有两个重载?以下定义不能满足所有目的吗?template::value>>T&&forward(consttypenamestd::remove_reference::type&val){returnstatic_cast(const_cast(val));}这里唯一的目的是conststd::remove_reference&服务是不复制。和enable_i

c++ - 为什么 std::forward 有两个重载?

鉴于以下引用折叠规则T&&-->T&T&&&-->T&T&&&-->T&T&&&&-->T&&第三条和第四条规则意味着T(refqualifer)&&是恒等变换,即T&停留在T&和T&&停留在T&&.为什么std::forward有两个重载?以下定义不能满足所有目的吗?template::value>>T&&forward(consttypenamestd::remove_reference::type&val){returnstatic_cast(const_cast(val));}这里唯一的目的是conststd::remove_reference&服务是不复制。和enable_i

c++ - unique_ptr、pimpl/forward 声明和完整定义

我已经查看了问题here和here,但仍然无法找出问题所在。这是调用代码:#include"lib.h"usingnamespacelib;intmain(constintargc,constchar*argv[]){return0;}这是库代码:#ifndeflib_h#definelib_h#include#include#includenamespacelib{classFoo_impl;classFoo{public:Foo();~Foo();private:Foo(constFoo&);Foo&operator=(constFoo&);std::unique_ptrm_imp

c++ - unique_ptr、pimpl/forward 声明和完整定义

我已经查看了问题here和here,但仍然无法找出问题所在。这是调用代码:#include"lib.h"usingnamespacelib;intmain(constintargc,constchar*argv[]){return0;}这是库代码:#ifndeflib_h#definelib_h#include#include#includenamespacelib{classFoo_impl;classFoo{public:Foo();~Foo();private:Foo(constFoo&);Foo&operator=(constFoo&);std::unique_ptrm_imp

c++ - 将 optional 与 reference_wrapper 结合起来有意义吗?

我突然想到在C++中可以使用类型std::optional>.这种类型的对象本质上是对T类型对象的引用。或空值,即几乎是一个指针。我的问题:std::optional>之间有什么概念上的区别吗?和T*?有什么实际区别吗?是否存在建议选择std::optional>的情况?超过T*? 最佳答案 Isthereanyconceptualdifferencebetweenstd::optional>andT*?std::optional,正如名称已经暗示的那样,当我们可以有一个值或可能根本没有任何值时使用。对于T*相当于没有值(value

c++ - 将 optional 与 reference_wrapper 结合起来有意义吗?

我突然想到在C++中可以使用类型std::optional>.这种类型的对象本质上是对T类型对象的引用。或空值,即几乎是一个指针。我的问题:std::optional>之间有什么概念上的区别吗?和T*?有什么实际区别吗?是否存在建议选择std::optional>的情况?超过T*? 最佳答案 Isthereanyconceptualdifferencebetweenstd::optional>andT*?std::optional,正如名称已经暗示的那样,当我们可以有一个值或可能根本没有任何值时使用。对于T*相当于没有值(value

c++ - 为什么 `std::reference_wrapper` 秒内不能推导出模板实例?

假设我有一些T类型的对象,我想把它放到一个引用包装器中:inta=5,b=7;std::reference_wrapperp(a),q(b);//or"autop=std::ref(a)"现在我可以轻松地说if(p,因为引用包装器具有到其包装类型的转换。一切都很好,我可以像处理原始对象一样处理引用包装器的集合。(正如questionlinkedbelow所示,这可能是生成现有集合的替代View的有用方法,可以随意重新排列而不产生完整拷贝的成本,以及维护更新与原始集合的完整性。)但是,对于某些类,这不起作用:std::strings1="hello",s2="world";std::re

c++ - 为什么 `std::reference_wrapper` 秒内不能推导出模板实例?

假设我有一些T类型的对象,我想把它放到一个引用包装器中:inta=5,b=7;std::reference_wrapperp(a),q(b);//or"autop=std::ref(a)"现在我可以轻松地说if(p,因为引用包装器具有到其包装类型的转换。一切都很好,我可以像处理原始对象一样处理引用包装器的集合。(正如questionlinkedbelow所示,这可能是生成现有集合的替代View的有用方法,可以随意重新排列而不产生完整拷贝的成本,以及维护更新与原始集合的完整性。)但是,对于某些类,这不起作用:std::strings1="hello",s2="world";std::re