BewareMyPower commented on code in PR #169:
URL: https://github.com/apache/pulsar-client-cpp/pull/169#discussion_r1109613473


##########
lib/ProducerInterceptors.h:
##########
@@ -0,0 +1,56 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <pulsar/ProducerInterceptor.h>
+
+#include <atomic>
+#include <utility>
+#include <vector>
+
+namespace pulsar {
+
+class ProducerInterceptors {
+   public:
+    explicit ProducerInterceptors(std::vector<ProducerInterceptorPtr> 
interceptors)
+        : interceptors_(std::move(interceptors)) {}

Review Comment:
   Even if you use `std::move(interceptors)`, the copy will still happen 
because this constructor accepts an argument by value, not reference.
   
   For example,
   
   ```c++
   #include <iostream>
   #include <vector>
   using namespace std;
   
   struct Foo {
     Foo() { cout << "Foo()" << endl; }
     Foo(const Foo &) { cout << "Foo copy" << endl; }
     Foo(Foo &&foo) noexcept { cout << "Foo move" << endl; }
   };
   
   struct FooVector {
     FooVector(vector<Foo> v) : v_(std::move(v)) {}
   
     vector<Foo> v_;
   };
   
   int main(int argc, char* argv[]) {
     vector<Foo> v;
     v.push_back(Foo{});
     cout << "--------" << endl;
     FooVector foos{v};
     return 0;
   }
   ```
   
   You will see the following output:
   
   ```
   Foo()
   Foo move
   --------
   Foo copy
   ```
   
   If you want to avoid the copy, you have to pass `std::move(v)` like:
   
   ```c++
     FooVector foos{std::move(v)};
   ```
   
   However, in your case, you call it here:
   
   ```c++
   auto interceptors = 
std::make_shared<ProducerInterceptors>(conf.getInterceptors());
   ```
   
   If you changed it to `std::move(conf.getInterceptors())`, the `conf` will 
hold no interceptor and an unexpected behavior might happen (imagine users 
might retrieve the interceptors from the `ProducerConfiguration` instance).
   
   Therefore, in short, it's better just to keep the copy semantics instead of 
the move semantics:
   
   ```c++
   explicit ProducerInterceptors(const std::vector<ProducerInterceptorPtr>& 
interceptors) : interceptors_(interceptors) {}
   ```
   
   This is not a critical path, so copying is acceptable here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to