Fix -Wpointer-bool-conversion warning in FunctionView LLVM PR 204944 updated Clang's -Wpointer-bool-conversion and -Wtautological-pointer-compare to look through lvalue references to functions and warn when they are evaluated in a boolean context or compared against null. When passing a function designator/name without '&' to webrtc::FunctionView, template argument deduction infers the type as an lvalue reference to a function. Previously, FunctionView used a single constructor for both function pointers and function references, which checked 'f ? ... : nullptr'. Evaluating a function reference in this ternary check triggered -Wpointer-bool-conversion. This change splits the constructor into two mutually exclusive constructors: 1. One for function pointers (using std::is_pointer), retaining the null check. 2. One for function references (using std::is_function), initializing call_ directly without the null check since function references can never be null. Bug: chromium:531796076 Change-Id: Ibb798adddb47c5a110232e4caf956eba2a0743fe Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/488020 Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Commit-Queue: Zequan Wu <zequanwu@google.com> Reviewed-by: Denise Tell <dct@google.com> Cr-Commit-Position: refs/heads/main@{#48172}
diff --git a/api/function_view.h b/api/function_view.h index adbb5cd..1c25bed 100644 --- a/api/function_view.h +++ b/api/function_view.h
@@ -73,14 +73,26 @@ // result is an empty FunctionView. template < typename F, - typename std::enable_if<std::is_function<typename std::remove_pointer< - typename std::remove_reference<F>::type>::type>::value>::type* = + typename std::enable_if< + std::is_pointer<typename std::remove_reference<F>::type>::value && + std::is_function<typename std::remove_pointer< + typename std::remove_reference<F>::type>::type>::value>::type* = nullptr> FunctionView(F&& f) : call_(f ? CallFunPtr<typename std::remove_pointer<F>::type> : nullptr) { f_.fun_ptr = reinterpret_cast<void (*)()>(f); } + // Constructor that accepts function references. + template < + typename F, + typename std::enable_if<std::is_function< + typename std::remove_reference<F>::type>::value>::type* = nullptr> + FunctionView(F&& f) + : call_(CallFunPtr<typename std::remove_reference<F>::type>) { + f_.fun_ptr = reinterpret_cast<void (*)()>(f); + } + // Constructor that accepts nullptr. It creates an empty FunctionView. template <typename F, typename std::enable_if<std::is_same<