Quantcast
Channel: Active questions tagged return-value - Stack Overflow
Viewing all articles
Browse latest Browse all 207

Return type for *this in a builder-class method - lvalue vs rvalue refence

$
0
0

Suppose I have a "builder" class B which builds a class C, and looks somewhat like the following:

class B {public:  // ...  B& set_foo(Foo a_foo) { foo_ = std::move(a_foo); return *this; }  C build() const { /* ... */ }};

This is all nice and good ... until I think about what happens when my B instance is a temporary. How should I design B to work also as temporary/rvalue, not just as an lvalue?

  • Should I write a set_foo() && variant, returning a B&&? That would mean replicating lots of methods, and well, DRY.
  • Should I just return B&& to begin with?
  • Should I do something else?

Viewing all articles
Browse latest Browse all 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>