Improve efficiency of trivial toDelim calls
authorPhil Willoughby <philwill@fb.com>
Mon, 26 Jun 2017 14:08:25 +0000 (07:08 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 26 Jun 2017 14:23:02 +0000 (07:23 -0700)
Summary:
`toDelim` with a single parameter of the same type as the target type was
previously copy-constructing its result. It will now construct it with perfect
forwarding which is more efficient if the input is a temporary.

This brings `toDelim` into line with the similar implementations of `to` and
`tryTo`

Reviewed By: yfeldblum

Differential Revision: D5301427

fbshipit-source-id: 843a2d93384de88cce42f26da6562a1a6ed0dc9c

folly/Conv.h

index 6cc327056637e0525d5d3988b983a6cf397890b5..72518d808f5ad895ad04c33eadb5a87a711f5cc9 100644 (file)
@@ -986,11 +986,12 @@ to(Src value) {
  * toDelim<SomeString>(SomeString str) returns itself.
  */
 template <class Tgt, class Delim, class Src>
-typename std::enable_if<IsSomeString<Tgt>::value &&
-                            std::is_same<Tgt, Src>::value,
-                        Tgt>::type
-toDelim(const Delim& /* delim */, const Src& value) {
-  return value;
+typename std::enable_if<
+    IsSomeString<Tgt>::value &&
+        std::is_same<Tgt, typename std::decay<Src>::type>::value,
+    Tgt>::type
+toDelim(const Delim& /* delim */, Src&& value) {
+  return std::forward<Src>(value);
 }
 
 /**