DEDUCING this PATTERNSother words, a way to tell from within a member function whether the expression it’s invoked on is an lvalue or an rvalue; whether it is const or volatile; and the expression’s type." ## OVERVIEW • A bit0 码力 | 126 页 | 5.15 MB | 1 年前3
Forwarding Referencesjpg) ## V alue categories LVALUE glvalue that is not an xvalue  ## V alue categories LVALUE glvalue that is not an xvalue f(make_int()); // OK ## Passing temporary arguments void foo(const int& x) foo(make_int()); • lvalue references to const CAN'T be mutated • True even if we pass a temporary (rvalue) ## Passing foo(const int& x) $$ \text{int temp} = x; $$ $$ x = 0; \quad // ERROR $$ foo(make_int()); • lvalue references to const CAN'T be mutated • True even if we pass a temporary (rvalue) void foo(int&&0 码力 | 107 页 | 3.72 MB | 1 年前3
The Many Shades of reference_wrapperprocess(dialog); ## reference_wrapper models rebindable reference ## reference_wrapper closely matches lvalue references • it may refer to const objects • it does not bind to rvalue expressions • declaring } ## Create from non-const lvalue void f(vectorv) { reference_wrapper r = std::as_const(v[0]); } ## reference_wrapper only binds to lvalue reference_wrapper r = "foo"s; auto& r = "foo"s; // doesn't compile ## reference_wrapperonly binds to lvalue, too reference_wrapper r = "foo"s; // nope string const& r = "foo"s; 0 码力 | 49 页 | 575.61 KB | 1 年前3
Modern C++ Tutorial: C++11/14/17/20 On the Flystd::bind and std::placeholder ..... 41 3.3 rvalue Reference ..... 41 lvalue, rvalue, prvalue, xvalue ..... 41 rvalue reference and lvalue reference ..... 43 Move semantics ..... 45 Perfect forwarding above are variables that have been declared in the outer scope, so these capture methods capture the lvalue and not capture the rvalue. C++14 gives us the convenience of allowing the captured members to be std::function possible. ## lvalue, rvalue, prvalue, xvalue To understand what the rvalue reference is all about, you must have a clear understanding of the lvalue and the rvalue. lvalue, left value, as the0 码力 | 92 页 | 1.79 MB | 2 年前3
Quantifying Accidental Complexity: An empirical look at teaching and using C++else X*| |Caller arguments|Initialized object (l- or rvalue)| |Callee uses|x is treated as a const lvalue Except each definite last use preserves the arg's l/rvalue-ness (incl. can move from rvalue arg)| non-const lvalue| |Callee uses|x is treated as a const lvalue Except each definite last use preserves the arg's l/rvalue-ness (incl. can move from rvalue arg)|x is treated as a non-const lvalue If function arguments|Initialized object (I- or rvalue)|Initialized non-const lvalue|Any non-const lvalue| |Callee uses|x is treated as a const lvalue Except each definite last use preserves the arg's l/rvalue-ness0 码力 | 36 页 | 2.68 MB | 1 年前3
Back to Basics: Move Semanticsvalid state RVALUE REFERENCES ## Lvalue An lvalue is, roughly, something: • that can appear on the left side of an assignment • with a name • with an address ## Lvalue Examples int var; var = 52; const *buffer = '\0'; std::arraywidgets; draw(Widgets[123]); Lvalue Examples int var; var = 52; const std::string hello = "Hello"; std::string greeting = hello; *buffer = '\0'; std::arraywidgets; draw( widgets[123]); Lvalue Examples int var; var = 52; const std::string hello = "Hello"; const lvalues are std::string0 码力 | 142 页 | 1.02 MB | 1 年前3
Calling Functions A Tutorialstandard conversion sequence is assigned one of three ranks: 1) Exact match: no conversion required, lvalue-to-rvalue conversion, qualification conversion, function pointer conversion, (since C++17) user-defined sequence S1 is better than a standard conversion sequence S2 if a) S1 is a subsequence of S2, excluding lvalue transformations. The identity conversion sequence is considered a subsequence of any other conversion an rvalue while S2 binds an lvalue reference to an rvalue int i; int f1(); int g(const int&); // overload #1 int g(const int&); // overload #2 int j = g(i); // lvalue int -> const int& is0 码力 | 111 页 | 5.11 MB | 1 年前3
Can't we just synthesize std::tuple_element from get?using type_lvalue = decltype(get(std::declval()); using type_rvalue = decltype(get(std::declval ## Problem 3: type is “reference to std::tuple_element::type” is introduced: lvalue reference if its corresponding initializer is an lvalue, rvalue reference otherwise. ## Why do we need to specialize std::tuple_element()); static constexpr auto stores_reference = std::same_as lvalue, type_rvalue="">; std::conditional_t< stores_reference, type_lvalue, std::remove_reference_t lvalue,>lvalue> }; }; lvalue>0 码力 | 31 页 | 282.80 KB | 1 年前3
Back To Basics: Rvalues and Move Semanticsstr2 = str1; Let’s call a reference that would still be alive after the end of the statement. ## Lvalue reference ## Let's name them std::string str2 = str2 + str1; Let’s call a reference that would reference Now, we need a language symbol for each... ## Lvalue reference std::string str2 = str1; We already have a language symbol for this one: ## Lvalue reference std::string str2 = str1; We already have to std::move an lvalue reference? templatevoid foo(T& a, T& b) { T temp = std::move(a); // do some more stuff } ## I s it valid to std::move an lvalue reference? template0 码力 | 80 页 | 740.53 KB | 1 年前3
Back to Basics Castingnoexcept ## std::forward void f(int const &arg) { puts("by lvalue"); } void f(int &arg) { puts("by rvalue"); } template< typename T > void func(T&& template when passing to another function ## std::forward void f(int const &arg) { puts("by lvalue"); } void f(int &arg) { puts("by rvalue"); } template< typename T > void func(T&& Returned: call with rvalue: std::forward: by rvalue normal: by lvalue ## std::forward void f(int const &arg) { puts("by lvalue"); } void f(int && arg) { puts("by rvalue");0 码力 | 117 页 | 1.57 MB | 1 年前3
共 99 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
相关搜索词
Deducing thistemplate deductionmember functionvalue categorylvaluervalue完美转发转发引用std::forwardreference_wrapperstd::functionlvalue referencesrvalue expressionsnullableModern C++C++11/14/17/20nullptrconstexprstructured bindingsaccidental complexityC++complexitysimplificationreductionrvalue referencesstd::movemove constructormove assignmentunique_ptr函数模板重载解析隐式参数成员函数SFINAEstd::tuple_elementgettemporarytypemove semanticsright value referencedynamic_castbit_caststatic_castRTTI













