Convert TODO to a comment on why a temporary is needed on the rv or writer()
BUG=none
TEST=none
Change-Id: Idc6ca0e402c5a236dd6c1282bd562d67aaf21866
Reviewed-on: https://gerrit.chromium.org/gerrit/11586
Commit-Ready: Gaurav Shah <gauravsh@chromium.org>
Reviewed-by: Gaurav Shah <gauravsh@chromium.org>
Tested-by: Gaurav Shah <gauravsh@chromium.org>
diff --git a/dbus_adaptor.cc b/dbus_adaptor.cc
index 67d4d73..ea1ada9 100644
--- a/dbus_adaptor.cc
+++ b/dbus_adaptor.cc
@@ -172,8 +172,19 @@
::DBus::MessageIter writer;
::DBus::Variant v;
- // TODO(quiche): figure out why we can't use operator<< without the
- // temporary variable.
+
+ // We have to use a local because the operator<< needs a reference
+ // to work on (the lhs) but writer() returns by-value. C++ prohibits
+ // initializing non-const references from a temporary.
+ // So:
+ // v.writer() << value;
+ // would NOT automagically promote the returned value of v.writer() to
+ // a non-const reference (if you think about it, that's almost always not what
+ // you'd want. see: http://gcc.gnu.org/ml/gcc-help/2006-04/msg00075.html).
+ //
+ // One could consider changing writer() to return a reference, but then it
+ // changes writer() semantics as it can not be a const reference. writer()
+ // currently doesn't modify the original object on which it's called.
writer = v.writer();
writer << value;
return v;
@@ -213,8 +224,7 @@
::DBus::MessageIter writer;
::DBus::Variant v;
- // TODO(quiche): figure out why we can't use operator<< without the
- // temporary variable.
+ // See note above on why we need to use a local.
writer = v.writer();
writer << value;
return v;