ref-qualifier

How do I remove code duplication between similar ref-qualified member functions?

自古美人都是妖i 提交于 2019-12-07 18:26:31
问题 Similarly to How do I remove code duplication between similar const and non-const member functions?, I want to remove the code duplication between nearly identical member functions, except for ref qualifiers. Let's say I have a class that's something like this: class MyStringBuilder { std::string member; public: // Other functions std::string create() const& { // Some work std::string result = member; // More work return result; } std::string create() && { // Some work std::string result =

How do I remove code duplication between similar ref-qualified member functions?

家住魔仙堡 提交于 2019-12-06 07:54:26
Similarly to How do I remove code duplication between similar const and non-const member functions? , I want to remove the code duplication between nearly identical member functions, except for ref qualifiers. Let's say I have a class that's something like this: class MyStringBuilder { std::string member; public: // Other functions std::string create() const& { // Some work std::string result = member; // More work return result; } std::string create() && { // Some work std::string result = std::move(member); // More work return result; } }; It's not inconceivable that we would want to do this

rvalue ref-qualifiers for STL containers

倾然丶 夕夏残阳落幕 提交于 2019-12-04 04:10:17
问题 Why element access member functions of STL containers, e.g. std::array::operator[] or std::vector::operator[] do not have rvalue ref-qualifier overloads? Of course I can do std::move(generate_vector()[10]) , but I'm curious if adding rvalue ref-qualifier overloads was considered when ref-qualifiers were being standardized. I think std::array<T, N> and std::tuple<T, T, ..., T> are really the same thing, and the "element access function (i.e., std::get )" of the latter is overloaded for all

ref-qualified member functions as template arguments?

我怕爱的太早我们不能终老 提交于 2019-12-04 03:01:49
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 sorts of unexpected compiler behaviour like crashing or internal compiler errors. I understand ref

call of overloaded with ref-qualifiers member function is ambiguous

蓝咒 提交于 2019-12-04 01:21:15
问题 I found a strange behaviour, when compliling my code with G++ ( gcc 4.8.1 and MinGW 4.8.2 with -std=gnu++1y flag). In spirit of SSCCE I isolating the following snippet: struct C { template< typename X > auto f(X &&) const & { ; } template< typename X > auto f(X &&) & { ; } template< typename X > auto f(X &&) && { ; } }; int main() { int i{}; #if 1 C{}.f(i); #endif #if 1 C c{}; c.f(i); #endif return 0; } It gives an error: main.cpp: In function 'int main()': main.cpp:29:10: error: call of

Explicit ref-qualified conversion operator templates in action

天大地大妈咪最大 提交于 2019-12-03 13:09:24
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& bl(al); // 4. OK const B& bz(al); // 5. OK const B& bc(ac); // 6. OK B cm(std::move(a)); // 7. error

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

こ雲淡風輕ζ 提交于 2019-12-03 05:00:02
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 ? 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. However string b = string("foo") += "bar"; is legal if operator += is declared as string & operator +=

Overload resolution with ref-qualifiers

Deadly 提交于 2019-11-27 08:19:12
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() const && { std::cout << "const rvalue" << std::endl; return std::move(_bar); } int _bar; }; int main(int