#const&&

25 messages · Page 1 of 1 (latest)

waxen parcel
#

is a const&& method overload a code smell? ie is there a way of calling such an overload from somewhere that is itself not a code smell? eg this overload set should be sufficient for anything irl:

struct S {
  auto foo() &;
  auto foo() const&;
  auto foo() &&;
  // auto foo() const&&; // is there any use for this?
};

are there legitimate ways to call foo() const&& except from, eg, withing an expression in which a function returns const S, which is already smelly?

const S bar(); // this is already smelly
int main() {
  bar().foo(); // will call const&& if it exists, otherwise const&.
}
south mesaBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

copper carbon
waxen parcel
#

UAF is already UB, so why do I care which will be chosen?

copper carbon
#

whose code is this and why did they write so many member function overloads?

#

how much do the different overloads differ?

waxen parcel
#

there are so many overloads because they are necessary to optimize for different cases. ie if foo is called on a temporary, then i want the && overload to be called.

#

tbh all of this is going to be kinda moot eventually anyway, because of deducing this. but for pre-C++23 code, it has its relevance

copper carbon
#

or the && overload?

waxen parcel
#

that would use the normal && overload

copper carbon
#

well then maybe you should get rid of the const&&

#

since, you can't move things away from a const anyways

ripe kindle
#

not sure I'd consider it a code smell. it's just not very useful 🤷‍♂️

#

like, what would you do with such a thing?

waxen parcel
#

well isn't that the point? either it's unused, or is indicative of code elsewhere that is probably (though not necessarily) wrong

rigid ivy
#

for example, std::ref and std::cref have deleted const&& overloads

#

without it, it would simply call the const& overload which would silently work and may be a mistake

waxen parcel
#

ok, yea that makes sense. it's like calling out a code smell explicitly

#

if you somehow got a const&&, that's wrong and should be called out

rigid ivy
#

kinda depends, it may be convenient to std::move a T which can potentially be const in some places

#

which would produce a const&& but that's not necessarily a mistake, not if your container supports const template arguments

waxen parcel
#

ok, thanks. that clears things up a bit

#

!solved