Questions tagged [ref-qualifier]

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

33 questions
22
votes
1 answer

Overload resolution with ref-qualifiers

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 #include struct foo { int& bar() & { std::cout…
K-ballo
  • 76,488
  • 19
  • 144
  • 164
18
votes
2 answers

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

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 ?
tommyk
  • 2,853
  • 4
  • 33
  • 54
16
votes
2 answers

Why template argument cannot be deduced in this context?

Could anyone explain why compilers (g++, visual c++) fail to deduce the template argument in this case? struct MyClass { void Foo(int x)& {} void Foo(int x)&& {} }; template void CallFoo(void(T::*func)(int)&) { //create…
14
votes
3 answers

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

In the following C++11+ code which return statement construction should be preferred? #include struct Bar { }; struct Foo { Bar bar; Bar get() && { return std::move(bar); // 1 return bar; // 2 …
Constructor
  • 6,903
  • 2
  • 17
  • 55
11
votes
1 answer

Forwarding cv-ref-qualifier for member functions

If there are no another overloadings (say, f(T &) or f(volatile T &&)) of a (member) function template template< typename T > f(T &&);, then T && is so-called forwarding reference, and T is either U, or U & for some cv-qualified type U. But for…
Tomilov Anatoliy
  • 13,614
  • 8
  • 46
  • 134
9
votes
1 answer

ref-qualified member functions as template arguments?

This compiles fine in clang 3.3: template struct M; template struct M { }; template struct M { }; but fails in gcc…
iavr
  • 7,277
  • 1
  • 13
  • 50
8
votes
1 answer

Explicit ref-qualified conversion operator templates in action

Given the following conversion operators struct A { template explicit operator T&& () &&; template explicit operator T& () &; template explicit operator const T& () const&; }; struct B…
iavr
  • 7,277
  • 1
  • 13
  • 50
7
votes
1 answer

Why can't a destructor have reference qualifiers?

Is there a reason (other than because the standard says so) to why the following code is not allowed? struct Foo { ~Foo() && {} ~Foo() & {} }; I know that it is illegal, but I wanna know why. I was thinking about the good old avoid unnamed…
Timo
  • 7,388
  • 2
  • 18
  • 44
7
votes
1 answer

call of overloaded with ref-qualifiers member function is ambiguous

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 & { ; } …
Tomilov Anatoliy
  • 13,614
  • 8
  • 46
  • 134
6
votes
1 answer

Is it reasonable to overload operators by ref-qualifier to prevent temporaries?

It just occurred to me that operator+ & co can operate on this for rvalues; i.e. given a class C, it's possible to do this: class C { // ... C operator-( const C& rhs ) const & { C result = *this; result -= rhs; …
Mr. Wonko
  • 630
  • 9
  • 17
5
votes
1 answer

rvalue ref-qualifiers for STL containers

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…
Junekey Jeon
  • 1,369
  • 1
  • 9
  • 18
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
  • 352
  • 9
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
  • 12,203
  • 5
  • 28
  • 59
4
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"; …
4
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,375
  • 19
  • 24
1
2 3