reference-wrapper

Why does std::reference_wrapper<const T> not accept a temporary?

早过忘川 提交于 2020-01-01 07:59:45
问题 Normally, rvalues can bind to const references ( const SomeType& ). It's built into the language. However, std::reference_wrapper<const T> does not accept an rvalue as its constructor argument since the corresponding overload is deliberately deleted. What is the reason for this inconsistency? std::reference_wrapper is "advertised" as the alternative to a reference variable for cases when we must pass by value but would like to preserve reference semantics. In other words, if the rvalue to

std::reference_wrapper and polymorphic containers

佐手、 提交于 2019-12-25 09:09:31
问题 I am trying to make a polymorphic vector using std::reference_wrapper for these classes: struct Int2TypeBase{ virtual void which(){ std::cout << "Int2TypeBase" << "\n";} }; template <int v> struct Int2Type : public Int2TypeBase { enum { value = v }; void which(){ std::cout << "Int2Type<" << value << ">""\n";} friend bool operator==(const Int2Type& lhs, const Int2Type& rhs){ return lhs.v == rhs.v; } }; Now I am trying to make use of std::reference_wrapper like this: int main(){ using namespace

std::reference_wrapper and polymorphic containers

爱⌒轻易说出口 提交于 2019-12-25 09:07:10
问题 I am trying to make a polymorphic vector using std::reference_wrapper for these classes: struct Int2TypeBase{ virtual void which(){ std::cout << "Int2TypeBase" << "\n";} }; template <int v> struct Int2Type : public Int2TypeBase { enum { value = v }; void which(){ std::cout << "Int2Type<" << value << ">""\n";} friend bool operator==(const Int2Type& lhs, const Int2Type& rhs){ return lhs.v == rhs.v; } }; Now I am trying to make use of std::reference_wrapper like this: int main(){ using namespace

reference_wrapper Referencing Primitive

别等时光非礼了梦想. 提交于 2019-12-25 08:15:53
问题 I was under the impression that I could use reference_wrapper to generate a functor that would return the object passed into the reference_wrapper ctor. But this isn't working. Am I doing it wrong? If so is there a better way to accomplish this? I can write a lambda, it just seems like I shouldn't have to. #include <iostream> #include <functional> using namespace std; void funPtrPrinter( function< int( void ) > output ) { cout << output() << endl; } int main( void ) { int thirteen = 13; auto

Accessing reference wrapper elements in vector c++11

淺唱寂寞╮ 提交于 2019-12-11 11:28:36
问题 In Graph class: typedef std::pair<double, Node&> PIRV; In another class that uses graph: typedef std::priority_queue<Graph::PIRV&, vector<std::reference_wrapper<Graph::PIRV>>, compareEdge> PECMP; Now I am trying to access the first element in the priority queue ( PECMP someQueue ) by doing double a = someQueue.top().first However I get the following error: error: ‘const value_type’ has no member named ‘first’ What is the better way to access elements stored in reference wrapper? Thanks 回答1:

Semantics for wrapped objects: reference/value by default via std::move/std::ref

旧城冷巷雨未停 提交于 2019-12-09 02:15:31
问题 In recent times I am using often a natural idiom I "discovered" in C++11 that is that wrapped object can automatically hold reference when this is possible. The main question here will be about the comparison with the behavior of this "idiom" to other behaviors in the standard (see below). For example: template<class T> struct wrap{ T t; }; template<class T> wrap<T> make_wrap(T&& t){ return wrap{std::forward<T>(t)}; } In this way for the code double a = 3.14 double const c = 3.14 I get,

Why standard containers use function templates instead of non-template Koenig operators

自作多情 提交于 2019-12-04 03:01:42
问题 This question is inspired by Issue with std::reference_wrapper. Let' say, for example, operator< for std::vector . It's defined as a function template as template< class T, class Alloc > bool operator<( const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs ); As a result, implicit conversion of function argument to the type of the corresponding function parameter is denied (basically because of its template nature). This greatly reduces the usefulness and convenience of std::reference

Semantics for wrapped objects: reference/value by default via std::move/std::ref

半腔热情 提交于 2019-12-01 01:59:22
In recent times I am using often a natural idiom I "discovered" in C++11 that is that wrapped object can automatically hold reference when this is possible. The main question here will be about the comparison with the behavior of this "idiom" to other behaviors in the standard (see below). For example: template<class T> struct wrap{ T t; }; template<class T> wrap<T> make_wrap(T&& t){ return wrap{std::forward<T>(t)}; } In this way for the code double a = 3.14 double const c = 3.14 I get, typeid( make_wrap(3.14) ) --> wrap<double> typeid( make_wrap(a) ) --> wrap<double&> typeid( make_wrap(c) ) -

Issue with std::reference_wrapper

此生再无相见时 提交于 2019-11-29 09:14:05
The issue is clear with the following code: #include <functional> #include <iostream> #include <vector> int main() { //std::vector<int> a, b; int a = 0, b = 0; auto refa = std::ref(a); auto refb = std::ref(b); std::cout << (refa < refb) << '\n'; return 0; } If I use the commented std::vector<int> a, b; instead of int a = 0, b = 0; , then the code does not compile on any of GCC 5.1, clang 3.6, or MSVC'13. In my opinion, std::reference_wrapper<std::vector<int>> is implicitly convertible to std::vector<int>& which is LessThanComparable, and thus it should be LessThanComparable itself. Could

Issue with std::reference_wrapper

ε祈祈猫儿з 提交于 2019-11-28 02:39:38
问题 The issue is clear with the following code: #include <functional> #include <iostream> #include <vector> int main() { //std::vector<int> a, b; int a = 0, b = 0; auto refa = std::ref(a); auto refb = std::ref(b); std::cout << (refa < refb) << '\n'; return 0; } If I use the commented std::vector<int> a, b; instead of int a = 0, b = 0; , then the code does not compile on any of GCC 5.1, clang 3.6, or MSVC'13. In my opinion, std::reference_wrapper<std::vector<int>> is implicitly convertible to std: