Skip to main content
deleted 50 characters in body
Source Link
Puppy
  • 147.5k
  • 40
  • 271
  • 481

What you have is overly complicated. First off, your understanding of temporary objects is wrong. Message() is a perfectly mutable value. It's just that it cannot bind to the lvalue reference, on account of being an rvalue.

If you really want to handle mutable lvalues and rvalues alike (and it's debatable whether that isn't a symptom of some other design problems), then you should simply have two function overloads:

void handleMessage(Message & m) { handleImpl(m); } void handleMessage(Message && m) { handleImplhandleMessage(m); } void handleImpl(Message & m) { /* do stuff */ } 

What you have is overly complicated. First off, your understanding of temporary objects is wrong. Message() is a perfectly mutable value. It's just that it cannot bind to the lvalue reference, on account of being an rvalue.

If you really want to handle mutable lvalues and rvalues alike (and it's debatable whether that isn't a symptom of some other design problems), then you should simply have two function overloads:

void handleMessage(Message & m) { handleImpl(m); } void handleMessage(Message && m) { handleImpl(m); } void handleImpl(Message & m) { /* do stuff */ } 

What you have is overly complicated. First off, your understanding of temporary objects is wrong. Message() is a perfectly mutable value. It's just that it cannot bind to the lvalue reference, on account of being an rvalue.

If you really want to handle mutable lvalues and rvalues alike (and it's debatable whether that isn't a symptom of some other design problems), then you should simply have two function overloads:

void handleMessage(Message & m) { handleImpl(m); } void handleMessage(Message && m) { handleMessage(m); } 
Source Link
Kerrek SB
  • 480.5k
  • 96
  • 904
  • 1.1k

What you have is overly complicated. First off, your understanding of temporary objects is wrong. Message() is a perfectly mutable value. It's just that it cannot bind to the lvalue reference, on account of being an rvalue.

If you really want to handle mutable lvalues and rvalues alike (and it's debatable whether that isn't a symptom of some other design problems), then you should simply have two function overloads:

void handleMessage(Message & m) { handleImpl(m); } void handleMessage(Message && m) { handleImpl(m); } void handleImpl(Message & m) { /* do stuff */ }