Questions tagged [ref-qualifier]

A way to specify the value category of the *this pointer in a member function.

47 questions
5
votes
1 answer

LValue ref qualified member function being called on an RValue object

I'm trying to figure out why the following snippet calls the LValue cast operator overload: #include class Foo { public: Foo(int i = 0) : i(i) {} operator const int& () const & { std::cout << "lvalue\n"; …
5
votes
0 answers

Perfect forwarding and ref-qualifiers for member functions

C++11 introduced the ability to ref-qualify member functions, as well as perfect forwarding. But can we mix them together? Consider this (working) example: struct bar{ std::string str; void do_stuff() & { /* version 1 */ } …
Bernard
  • 5,209
  • 1
  • 34
  • 64
4
votes
0 answers

What is the rationale behind std::make_[un]signed to copy cv but not ref qualifiers

While trying to implement a make_signed_with_fallback for non integral types i noticed that std::make_[un]signed is designed to copy cv but not ref qualifiers and I'm wondering why. The only explanation I can think of is that copying ref qualifiers…
jesses
  • 559
  • 3
  • 15
4
votes
1 answer

Returning by value or by rvalue reference from rvalue reference qualified member function?

In Effective Modern C++, Item 12, Scott Meyers writes the following class to show how useful overloading member functions on the reference qualifiers can be: class Widget { public: using DataType = std::vector; … DataType& data()…
Enlico
  • 23,259
  • 6
  • 48
  • 102
4
votes
1 answer

ref-qualifiers for the assignment operator of standard library types

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"} =…
4
votes
2 answers

Trailing return type, declval and reference qualifiers: can they work together?

Consider the following example: #include struct A { void f() {} }; struct B { void f() & {} }; struct C { void f() && {} }; template auto f() -> decltype(std::declval().f()) {} int main() { f(); // f(); //…
skypjack
  • 49,335
  • 19
  • 95
  • 187
4
votes
1 answer

Reference qualifiers and deleted member methods

Consider the following code: #include struct S { void f(int) = delete; void f(int) && { } }; int main() { } It doesn't compile saying that the member method cannot be overloaded and it makes sense, of course. On the other side,…
skypjack
  • 49,335
  • 19
  • 95
  • 187
3
votes
1 answer

When are ref-qualified member functions necessary

As I understand it, ref-qualified member-functions are used to distinguish between operating on an implicit this as an lvalue versus an rvalue. If we define a member function without the ref-qualification, then both lvalues and rvalues call the same…
John James
  • 219
  • 1
  • 8
3
votes
1 answer

Generating the necessary ref-qualified overloads for a member function

I have this class: template class Array { private: T array[N]; public: template constexpr Array(InitValues... init_values) : array{ init_values... } {} …
Alex Vergara
  • 1,766
  • 1
  • 10
  • 29
3
votes
0 answers

C++ user-defined conversions, ref-qualifiers and overload resolution

Please, help me understand what's wrong with this piece of code: #include #include class Sample { public: explicit Sample(std::string data): _data(std::move(data)) {} operator const std::string &() const & {…
3
votes
2 answers

implicit object parameter and this pointer

With reference to Non-static member functions, under const-, volatile-, and ref-qualified member functions it is mentioned: A non-static member function can be declared with no ref-qualifier, with an lvalue ref-qualifier (the token & after the…
Vinod
  • 925
  • 8
  • 9
3
votes
0 answers

WG21 rationale for not using ref-qualifiers

Which WG21 documents explain the decision not to include ref-qualifiers in most standard library classes? An example that would benefit from such inclusion: template C1 container_cast(C2&& source) { C1 dest; // if…
Roman Odaisky
  • 2,811
  • 22
  • 26
3
votes
1 answer

Why is void B::f() const & chosen when B::f is called by a temporary object of B?

#include struct A { void f() const & { std::cout << "A::f()&" << std::endl; } void f() const && { std::cout << "A::f()&&" << std::endl; } }; struct B { void f() const & { …
xmllmx
  • 39,765
  • 26
  • 162
  • 323
3
votes
1 answer

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

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…
Constructor
  • 7,273
  • 2
  • 24
  • 66
3
votes
1 answer

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

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…
Justin
  • 24,288
  • 12
  • 92
  • 142