54 questions
2
votes
1
answer
136
views
Must T be unqualified in std::allocator<T>?
gcc, clang, and msvc all reject the following code:
#include <memory>
#include <vector>
int main() {
auto _ = std::vector<int const>{}; // error
auto _ = std::vector<...
1
vote
1
answer
124
views
How to replace ref-qualifiers of member functions with concept?
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 ...
0
votes
1
answer
86
views
Is GCC correct in rejecting overload between ref-qualified and non-ref-qualified member function? [duplicate]
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()...
3
votes
1
answer
252
views
Overloading ref-qualified member-function without ref-qualifier
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+...
4
votes
1
answer
1k
views
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-...
2
votes
1
answer
110
views
What's the point of ref-qualified member functions in `std::optional::value`
These are the signatures, according to Cppreference:
constexpr T& value() &;
constexpr const T& value() const &;
constexpr T&& value() &&;
constexpr const T&& ...
3
votes
1
answer
285
views
Generating the necessary ref-qualified overloads for a member function
I have this class:
template<typename T, size_t N>
class Array {
private:
T array[N];
public:
template <typename... InitValues>
constexpr Array(InitValues......
0
votes
1
answer
152
views
Simplifying the use of ref qualifiers in c++20
#include <string>
struct S
{
std::string s_;
std::string_view get() const &
{
return s_;
}
std::string_view get() const && = delete;
};
// I can't ...
1
vote
1
answer
518
views
Conversion operator with ref-qualifers: rvalue ref and const lvalue ref overloads ambiguity
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 ...
2
votes
0
answers
121
views
Why `std::vector::reserve` is not l-value ref-qualified?
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?
3
votes
0
answers
154
views
C++ user-defined conversions, ref-qualifiers and overload resolution
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(...
0
votes
2
answers
93
views
Is there a use-case for std::string's operator= to not be lvalue ref-qualified?
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 ...
7
votes
1
answer
235
views
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 ...
1
vote
1
answer
137
views
Why do ref-qualifier together with cv-qualifier on operator overloading allow rvalue assignment?
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>
...
8
votes
1
answer
407
views
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 ...
4
votes
0
answers
161
views
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 ...
1
vote
1
answer
322
views
Wording of GCC's error message when calling lvalue-ref qualified member function on temporary object
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 &-...
4
votes
1
answer
883
views
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 =...
1
vote
0
answers
102
views
Bug in gcc-9 with implicit conversion via ref-qualified type-operators?
Consider this:
struct Man
{
operator int const& () const& { return m_ban ; }
operator int && () && { return std::move(m_ban); }
int m_ban;
};
void ...
2
votes
1
answer
410
views
c++ - const member func, that can be called upon lvalue instances only, using a ref-qualifier
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 ...
5
votes
2
answers
231
views
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
...
9
votes
1
answer
277
views
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 ...
5
votes
1
answer
1k
views
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 <iostream>
class Foo
{
public:
Foo(int i = 0) : i(i) {}
operator const int& () ...
2
votes
1
answer
90
views
Non-static member functions with no ref-qualifier
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 ...
4
votes
2
answers
1k
views
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,
...
3
votes
0
answers
102
views
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 <class C1, class C2>
C1 ...
12
votes
1
answer
635
views
What is the difference between getters with const, and with const& qualifiers?
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::...
6
votes
2
answers
237
views
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";
...
3
votes
1
answer
315
views
Why is void B::f() const & chosen when B::f is called by a temporary object of B?
#include <iostream>
struct A
{
void f() const &
{
std::cout << "A::f()&" << std::endl;
}
void f() const &&
{
std::cout << "...
1
vote
0
answers
71
views
Conditional ref qualifier
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 ...
7
votes
1
answer
1k
views
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()[...
3
votes
1
answer
1k
views
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 ...
16
votes
3
answers
1k
views
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 <utility>
struct Bar
{
};
struct Foo
{
Bar bar;
Bar get() &&
{
...
38
votes
2
answers
11k
views
What does the & (ampersand) at the end of member function signature mean? [duplicate]
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 ...
3
votes
1
answer
172
views
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 ...
5
votes
0
answers
132
views
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 ...
16
votes
2
answers
746
views
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<...
4
votes
2
answers
314
views
Trailing return type, declval and reference qualifiers: can they work together?
Consider the following example:
#include <utility>
struct A { void f() {} };
struct B { void f() & {} };
struct C { void f() && {} };
template<typename T>
auto f() -> ...
1
vote
0
answers
65
views
Reference as last symbol in C++ function declaration? [duplicate]
Looking at Boost::Optional optional class template header I come across this:
T const& operator*() const&
T& operator*() &;
T&& operator*() &&;
For the life ...
0
votes
2
answers
38
views
error of template instantiation of ref-qualified members
#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&)...
4
votes
1
answer
247
views
Reference qualifiers and deleted member methods
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 ...
12
votes
1
answer
713
views
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 ...
6
votes
1
answer
140
views
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 & {
...
1
vote
1
answer
128
views
Difference between l-value ref-qualified member function and unqualified member function?
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 ...
20
votes
2
answers
2k
views
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 ...
1
vote
0
answers
106
views
Can I make reference easily for more resources?
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 ...
1
vote
1
answer
299
views
Can I make reference for whole resource folder?
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 ...
3
votes
6
answers
417
views
How to improve the efficiency of "str1 + str2 + str3 + ..." in C++14?
std::string Concatenate(const std::string& s1,
const std::string& s2,
const std::string& s3,
const std::string& ...
8
votes
1
answer
807
views
Explicit ref-qualified conversion operator templates in action
Given the following conversion operators
struct A
{
template<typename T> explicit operator T&& () &&;
template<typename T> explicit operator T& () ...
7
votes
1
answer
491
views
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< ...