ref-qualifier

ref-qualifiers for the assignment operator of standard library types

本秂侑毒 提交于 2021-02-09 10:55:43
问题 I was wondering, is there a reason the assignment operator of standard types is not lvalue ref-qualified? None of them are. Because of that, we can write things such as this: std::string{} = "42"; std::string s = "hello " + std::string{"world"} = "oops!"; std::vector<int> v = { 1,2,3 }; std::move(v) = { 4,5,6 }; If the assignment operator was lvalue ref-qualified all of these examples would not compile. Is it because there's a lot of things to modify (but then so it was for noexcept) and

ref-qualifiers for the assignment operator of standard library types

牧云@^-^@ 提交于 2021-02-09 10:54:09
问题 I was wondering, is there a reason the assignment operator of standard types is not lvalue ref-qualified? None of them are. Because of that, we can write things such as this: std::string{} = "42"; std::string s = "hello " + std::string{"world"} = "oops!"; std::vector<int> v = { 1,2,3 }; std::move(v) = { 4,5,6 }; If the assignment operator was lvalue ref-qualified all of these examples would not compile. Is it because there's a lot of things to modify (but then so it was for noexcept) and

c++ - const member func, that can be called upon lvalue instances only, using a ref-qualifier

醉酒当歌 提交于 2021-02-05 07:48:31
问题 I'm trying to enforce a const 'getter' method of a class to be called upon only lvalue instances of the class, via a ref-qualifier and for some reason getting an unexpected result (I'm compiling with clang 6.0.1 with C++ 17 support, via c++1z flag, on Windows ): The declaration bool getVal() const &; allows the method to be called on rvalue references also . The declaration bool getVal() &; doesn't allow the method to be called on rvalue references BUT, as I understand - the function isn't a

To move, or not to move from r-value ref-qualified method?

 ̄綄美尐妖づ 提交于 2020-06-27 06:48:06
问题 In the following C++11+ code which return statement construction should be preferred? #include <utility> struct Bar { }; struct Foo { Bar bar; Bar get() && { return std::move(bar); // 1 return bar; // 2 } }; 回答1: Well, since it's a r-value ref qualified member function, this is presumably about to expire. So it makes sense to move bar out, assuming Bar actually gains something from being moved. Since bar is a member, and not a local object/function parameter, the usual criteria for copy

Is there any real use case for function's reference qualifiers?

独自空忆成欢 提交于 2020-01-01 01:27:05
问题 Recently I learned about function's reference qualifiers , e.g. struct foo { void bar() {} void bar1() & {} void bar2() && {} }; Where I might need this feature, is there any real use case for this language feature ? 回答1: There are basically two uses: To provide an optimized overload, for example to move a member out of a temporary object instead of having to copy it. Prevent misuse of an API. For example, no one would expect int a = 1 += 2; to work and this would also cause a compile error.

Explicit ref-qualified conversion operator templates in action

五迷三道 提交于 2019-12-21 04:27:17
问题 Given the following conversion operators struct A { template<typename T> explicit operator T&& () &&; template<typename T> explicit operator T& () &; template<typename T> explicit operator const T& () const&; }; struct B {}; I would expect the following conversions to be all valid, yet some give compile errors (live example): A a; A&& ar = std::move(a); A& al = a; const A& ac = a; B&& bm(std::move(a)); // 1. OK B&& bt(A{}); // 2. OK B&& br(ar); // 3. error: no viable conversion from A to B B&

Overload resolution with ref-qualifiers

流过昼夜 提交于 2019-12-17 09:40:37
问题 While working with ref-qualified function overloads, I'm getting different results from GCC (4.8.1) and Clang (2.9 and trunk) . Consider the following code: #include <iostream> #include <utility> struct foo { int& bar() & { std::cout << "non-const lvalue" << std::endl; return _bar; } //~ int&& bar() && //~ { //~ std::cout << "non-const rvalue" << std::endl; //~ return std::move(_bar); //~ } int const& bar() const & { std::cout << "const lvalue" << std::endl; return _bar; } int const&& bar()

ref-qualified member functions as template arguments?

筅森魡賤 提交于 2019-12-14 00:20:05
问题 This compiles fine in clang 3.3: template <typename T> struct M; template <typename R, typename C, typename... A> struct M <R (C::*)(A...)> { }; template <typename R, typename C, typename... A> struct M <R (C::*)(A...) &> { }; but fails in gcc 4.8.1: [...] error: redefinition of ‘struct M <R (C::*)(A ...)>’ struct M <R (C::*)(A...) &> { }; ^ [...] error: previous definition of ‘struct M <R (C::*)(A ...)>’ struct M <R (C::*)(A...)> { }; ^ When used in different contexts, this results in all

Is it a good practice to return the r-value reference from the r-value ref-qualified method?

这一生的挚爱 提交于 2019-12-11 03:02:50
问题 As I can see the general rule is not to return r-value references from functions at all (except for rare special cases). But what about class methods? There is an example in the C++ standard library of returning r-value reference from the r-value ref-qualified method of the class (std::optional<T>::operator*() and std::optional<T>::value() methods of the std::optional<T> class). See sections 23.6.3 Class template optional [optional.optional] and 23.6.3.5 Observers [optional.observe] of the C+