DarkSharpness commented on code in PR #266:
URL: https://github.com/apache/tvm-ffi/pull/266#discussion_r2532157599


##########
include/tvm/ffi/function.h:
##########
@@ -503,9 +513,9 @@ class Function : public ObjectRef {
    * \param name optional name attacked to the function.
    */
   template <typename TCallable>
-  static Function FromTyped(TCallable callable, std::string name) {
-    using FuncInfo = details::FunctionInfo<TCallable>;
-    auto call_packed = [callable = std::move(callable), name = 
std::move(name)](
+  static Function FromTyped(TCallable&& callable, std::string name) {
+    using FuncInfo = details::FunctionInfo<std::decay_t<TCallable>>;
+    auto call_packed = [callable = std::forward<TCallable>(callable), name = 
std::move(name)](

Review Comment:
   In lambda expression, a capture without `&` will always store by value 
(init-capture in 
[cppreference](https://en.cppreference.com/w/cpp/language/lambda.html)). So 
callable capture is by value (just as `std::decay_t<TCallable>` instead of by 
reference. The `forward` here just means we will choose to copy or move 
construct the inner-captured value.
   
   `FuncInfo` is inherited from the old code. It is used to get the return type 
and num of arguments:
   
   ```cpp
     template <typename TCallable>
     static Function FromTyped(TCallable&& callable) {
       using FuncInfo = details::FunctionInfo<std::decay_t<TCallable>>;
       auto call_packed = [callable = std::forward<TCallable>(callable)](
                              const AnyView* args, int32_t num_args, Any* rv) 
mutable -> void {
         details::unpack_call<typename FuncInfo::RetType>(
             std::make_index_sequence<FuncInfo::num_args>{}, nullptr, callable, 
args, num_args, rv);
       };
       return FromPackedInternal(std::move(call_packed));
     }
     ```



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to