reference-wrapper

Type emulating a C++ reference better than std::reference_wrapper

折月煮酒 提交于 2019-11-28 01:38:08
I am designing a class that behaves like a C++ references but does some extra instrumental stuff (bookkeeping). Initially I thought that std::reference_wrapper<T> would be a good model. But after a while I realized that std::reference_wrapper<T> doesn't behave, even in principle, as a C++ reference because assignment rebinds the internal pointer. double a = 5.; double b = 3.; double& ref = a; ref = b; assert(&ref != &b); // ref is not bound to b std::reference_wrapper<double> refwpr = a; refwpr = b; assert(&refwpr.get() == &b); // ref wrapper was rebound to b I can of course change that

Why can template instances not be deduced in `std::reference_wrapper`s?

試著忘記壹切 提交于 2019-11-27 23:33:53
Suppose I have some object of type T , and I want to put it into a reference wrapper: int a = 5, b = 7; std::reference_wrapper<int> p(a), q(b); // or "auto p = std::ref(a)" Now I can readily say if (p < q) , because the reference wrapper has a conversion to its wrapped type. All is happy, and I can process a collection of reference wrappers just like they were the original objects. (As the question linked below shows, this can be a useful way to produce an alternate view of an existing collection, which can be rearranged at will without incurring the cost of a full copy, as well as maintaining

Difference between std::reference_wrapper and simple pointer?

大城市里の小女人 提交于 2019-11-27 10:38:47
Why is there a need to have std::reference_wrapper ? Where should it be used? How is it different from a simple pointer? How its performance compares to a simple pointer? std::reference_wrapper is useful in combination with templates. It wraps an object by storing a pointer to it, allowing for reassignment and copy while mimicking its usual semantics. It also instructs certain library templates to store references instead of objects. Consider the algorithms in the STL which copy functors: You can avoid that copy by simply passing a reference wrapper referring to the functor instead of the

Type emulating a C++ reference better than std::reference_wrapper

六眼飞鱼酱① 提交于 2019-11-26 21:56:57
问题 I am designing a class that behaves like a C++ references but does some extra instrumental stuff (bookkeeping). Initially I thought that std::reference_wrapper<T> would be a good model. But after a while I realized that std::reference_wrapper<T> doesn't behave, even in principle, as a C++ reference because assignment rebinds the internal pointer. double a = 5.; double b = 3.; double& ref = a; ref = b; assert(&ref != &b); // ref is not bound to b std::reference_wrapper<double> refwpr = a;

Why can template instances not be deduced in `std::reference_wrapper`s?

三世轮回 提交于 2019-11-26 21:12:35
问题 Suppose I have some object of type T , and I want to put it into a reference wrapper: int a = 5, b = 7; std::reference_wrapper<int> p(a), q(b); // or "auto p = std::ref(a)" Now I can readily say if (p < q) , because the reference wrapper has a conversion to its wrapped type. All is happy, and I can process a collection of reference wrappers just like they were the original objects. (As the question linked below shows, this can be a useful way to produce an alternate view of an existing

Difference between std::reference_wrapper and simple pointer?

陌路散爱 提交于 2019-11-26 15:14:56
问题 Why is there a need to have std::reference_wrapper? Where should it be used? How is it different from a simple pointer? How its performance compares to a simple pointer? 回答1: std::reference_wrapper is useful in combination with templates. It wraps an object by storing a pointer to it, allowing for reassignment and copy while mimicking its usual semantics. It also instructs certain library templates to store references instead of objects. Consider the algorithms in the STL which copy functors: