Skip to main content
Filter by
Sorted by
Tagged with
2 votes
1 answer
136 views

gcc, clang, and msvc all reject the following code: #include <memory> #include <vector> int main() { auto _ = std::vector<int const>{}; // error auto _ = std::vector<...
xmllmx's user avatar
  • 44.6k
1 vote
1 answer
124 views

Is it possible to replace following ref-qualifiers with concepts? That is, instead of struct S { void func() && {} void func() & {} }; have something like this struct S { void ...
tommsch's user avatar
  • 778
0 votes
1 answer
86 views

If I understand this correctly, [over.load] doesn't exist in c++23, and so what I read in [over.load]/2.3 should not be true anymore, so this code struct Foo { int const& bar() const; int bar()...
Enlico's user avatar
  • 30.3k
3 votes
1 answer
252 views

Is the non ref-qualified overload of member-function f in accordance with the C++20 standard? Both Clang and GCC seem to accept it. Are they both pulling on the short end of the stick? What does the C+...
303's user avatar
  • 4,852
4 votes
1 answer
1k views

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-...
John James's user avatar
2 votes
1 answer
110 views

These are the signatures, according to Cppreference: constexpr T& value() &; constexpr const T& value() const &; constexpr T&& value() &&; constexpr const T&& ...
BIuesky's user avatar
  • 107
3 votes
1 answer
285 views

I have this class: template<typename T, size_t N> class Array { private: T array[N]; public: template <typename... InitValues> constexpr Array(InitValues......
Alex Vergara's user avatar
  • 2,335
0 votes
1 answer
152 views

#include <string> struct S { std::string s_; std::string_view get() const & { return s_; } std::string_view get() const && = delete; }; // I can't ...
zrb's user avatar
  • 891
1 vote
1 answer
518 views

While answering another question, I noticed something peculiar about conversion operators when dealing with ref-qualifiers. Consider the following code: using P = std::unique_ptr<int>; struct A ...
Nelfeal's user avatar
  • 13.3k
2 votes
0 answers
121 views

Are there any use cases for std::vector::reserve() on an r-value std::vector, or is reserve() not l-value ref-qualified only because of backward compatibility?
blonded04's user avatar
  • 531
3 votes
0 answers
154 views

Please, help me understand what's wrong with this piece of code: #include <string> #include <utility> class Sample { public: explicit Sample(std::string data): _data(std::move(...
Yurii A's user avatar
  • 379
0 votes
2 answers
93 views

The post here points out that std::string's member operator= is not lvalue ref-qualified. That allows us to write code such as this: std::string() = "Hello"; The linked post asks why this ...
user589321's user avatar
7 votes
1 answer
235 views

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 ...
Fedor's user avatar
  • 24.7k
1 vote
1 answer
137 views

Adding a ref-qualifier to an operator will remove the possibility to do rvalue assignment for example, compiling the following with g++ -std=c++14 bar.cpp && ./a.out #include <cstdio> ...
mattsson's user avatar
  • 1,174
8 votes
1 answer
407 views

The following code causes undefined behaviour: class T { public: const std::string& get() const { return s_; } private: std::string s_ { "test" }; } void breaking() { const ...
Tim's user avatar
  • 153
4 votes
0 answers
161 views

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 ...
jesses's user avatar
  • 631
1 vote
1 answer
322 views

The following code does not compile, expectedly: struct A { void doWork() & {} }; int main() { A{}.doWork(); } My understanding is that the temporary A{} cannot bind to the &-...
Enlico's user avatar
  • 30.3k
4 votes
1 answer
883 views

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 =...
Enlico's user avatar
  • 30.3k
1 vote
0 answers
102 views

Consider this: struct Man { operator int const& () const& { return m_ban ; } operator int && () && { return std::move(m_ban); } int m_ban; }; void ...
Vahagn's user avatar
  • 4,860
2 votes
1 answer
410 views

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 ...
golosovsky's user avatar
5 votes
2 answers
231 views

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 ...
cigien's user avatar
  • 61.2k
9 votes
1 answer
277 views

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 ...
Timo's user avatar
  • 9,985
5 votes
1 answer
1k views

I'm trying to figure out why the following snippet calls the LValue cast operator overload: #include <iostream> class Foo { public: Foo(int i = 0) : i(i) {} operator const int& () ...
Christopher Leong's user avatar
2 votes
1 answer
90 views

This is a follow-up to my previous post With reference to Non-static member functions Under const-, volatile-, and ref-qualified member functions A non-static member function can be declared with no ...
Vinod's user avatar
  • 1,215
4 votes
2 answers
1k views

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, ...
Vinod's user avatar
  • 1,215
3 votes
0 answers
102 views

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 <class C1, class C2> C1 ...
Roman Odaisky's user avatar
12 votes
1 answer
635 views

I have watched a talk (exact timestamp, not explained by him) by Nicolai Josuttis (member of C++ standard committee) and he stated that ever since C++11, getters should be written like so: const std::...
Croolman's user avatar
  • 1,143
6 votes
2 answers
237 views

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"; ...
Marius Bancila's user avatar
3 votes
1 answer
315 views

#include <iostream> struct A { void f() const & { std::cout << "A::f()&" << std::endl; } void f() const && { std::cout << "...
xmllmx's user avatar
  • 44.6k
1 vote
0 answers
71 views

I have CRTP-like hierarchy where derived classes may be implemented a bit differently and for one class some methods are allowed to call on rvalue references but for another class this would be ...
eXXXXXXXXXXX2's user avatar
7 votes
1 answer
1k views

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()[...
Junekey Jeon's user avatar
  • 1,597
3 votes
1 answer
1k views

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 ...
Constructor's user avatar
  • 7,551
16 votes
3 answers
1k views

In the following C++11+ code which return statement construction should be preferred? #include <utility> struct Bar { }; struct Foo { Bar bar; Bar get() && { ...
Constructor's user avatar
  • 7,551
38 votes
2 answers
11k views

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 ...
Martin B.'s user avatar
  • 1,743
3 votes
1 answer
172 views

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 ...
Justin's user avatar
  • 25.7k
5 votes
0 answers
132 views

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 ...
Bernard's user avatar
  • 5,800
16 votes
2 answers
746 views

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<...
John's user avatar
  • 161
4 votes
2 answers
314 views

Consider the following example: #include <utility> struct A { void f() {} }; struct B { void f() & {} }; struct C { void f() && {} }; template<typename T> auto f() -> ...
skypjack's user avatar
  • 50.9k
1 vote
0 answers
65 views

Looking at Boost::Optional optional class template header I come across this: T const& operator*() const& T& operator*() &; T&& operator*() &&; For the life ...
A-n-t-h-o-n-y's user avatar
0 votes
2 answers
38 views

#include <iostream> using std::cout; template<typename T> class A{ public: template<typename U> void f(U const&) & ; template<typename U> void f(U const&)...
Tatyana Fedorovna's user avatar
4 votes
1 answer
247 views

Consider the following code: #include<utility> struct S { void f(int) = delete; void f(int) && { } }; int main() { } It doesn't compile saying that the member method cannot be ...
skypjack's user avatar
  • 50.9k
12 votes
1 answer
713 views

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 ...
Tomilov Anatoliy's user avatar
6 votes
1 answer
140 views

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 & { ...
Mr. Wonko's user avatar
  • 760
1 vote
1 answer
128 views

Is there a difference between l-value ref-qualified member functions and unqualified member functions? If so, what is it? I.e., are these two ways of declaring func() different? class Foo { void ...
Kyle Strand's user avatar
  • 16.5k
20 votes
2 answers
2k views

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 ...
tommyk's user avatar
  • 3,357
1 vote
0 answers
106 views

I am developping a game. I would like a picture to be shown on the screen. It should fill the screen's 90% in width (about 90%). I don't want the system to resize my images (I don't like the quality ...
Tomi's user avatar
  • 3,588
1 vote
1 answer
299 views

I would like to support many different screen types with the drawable resources. But I can use some drawables with the same size on more screens. For example: -xxhdpi-normal and -mdpi-xlarge could ...
Tomi's user avatar
  • 3,588
3 votes
6 answers
417 views

std::string Concatenate(const std::string& s1, const std::string& s2, const std::string& s3, const std::string& ...
xmllmx's user avatar
  • 44.6k
8 votes
1 answer
807 views

Given the following conversion operators struct A { template<typename T> explicit operator T&& () &&; template<typename T> explicit operator T& () ...
iavr's user avatar
  • 7,667
7 votes
1 answer
491 views

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< ...
Tomilov Anatoliy's user avatar