Questions tagged [ref-qualifier]

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

47 questions
29
votes
2 answers

What does the & (ampersand) at the end of member function signature mean?

I can't remember which talk it was, but recently I watched some talks from CppCon 2017 and there someone mentioned as some kind of side-note, that the only true way of overloading operator= would be in the following fashion: class test { public: …
Martin B.
  • 1,567
  • 14
  • 26
27
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
  • 80,396
  • 20
  • 159
  • 169
19
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
  • 3,187
  • 7
  • 39
  • 61
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…
John
  • 161
  • 2
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
  • 7,273
  • 2
  • 24
  • 66
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
  • 15,657
  • 10
  • 64
  • 169
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,547
  • 1
  • 18
  • 53
8
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
  • 9,269
  • 2
  • 28
  • 58
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,547
  • 1
  • 18
  • 53
7
votes
1 answer

Overloading a parent member function without ref-qualifier with a child member function with ref-qualifier in C++

In C++ one cannot overload in one class a member function with ref-qualifier with a member function without ref-qualifier. But at the same time it is possible to inherit one member function from a parent class and overload it in a child class as in…
Fedor
  • 17,146
  • 13
  • 40
  • 131
7
votes
1 answer

Best practice for getters with ref-qualifier

The following code causes undefined behaviour: class T { public: const std::string& get() const { return s_; } private: std::string s_ { "test" }; } void breaking() { const auto& str = T{}.get(); // do sth with "str" <--…
Tim
  • 143
  • 1
  • 5
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
  • 15,657
  • 10
  • 64
  • 169
6
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,496
  • 1
  • 11
  • 18
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
  • 690
  • 9
  • 17
5
votes
2 answers

Why is std::string's member operator= not lvalue ref-qualified

I recently learned that member functions can be ref-qualified, which allows me to write struct S { S& operator=(S const&) & // can only be used if the implicit object is an lvalue { return *this; } }; S operator+(S const &, S…
cigien
  • 57,834
  • 11
  • 73
  • 112
1
2 3 4