[libcxx-commits] [PATCH] D116380: [libc++] Use std::addressof in std::function::target

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jan 4 09:32:05 PST 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rGe24ddb6027b6: [libc++] Use std::addressof in std::function::target (authored by ldionne).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116380/new/

https://reviews.llvm.org/D116380

Files:
  libcxx/include/__functional/function.h
  libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/addressof.pass.cpp
  libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/robust_against_adl.pass.cpp


Index: libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/robust_against_adl.pass.cpp
===================================================================
--- libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/robust_against_adl.pass.cpp
+++ libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/robust_against_adl.pass.cpp
@@ -27,8 +27,14 @@
 
 struct Incomplete;
 template<class T> struct Holder { T t; };
+
 typedef Holder<Incomplete> *Ptr;
 
+template<class T>
+struct Callable {
+    void operator()() const { }
+};
+
 Ptr no_args() { return nullptr; }
 Ptr one_arg(Ptr p) { return p; }
 Ptr two_args(Ptr p, Ptr) { return p; }
@@ -37,11 +43,11 @@
 
 void one_arg_void(Ptr) { }
 
-int main(int, char**)
-{
+int main(int, char**) {
     Ptr x = nullptr;
     std::function<Ptr()> f(no_args); f();
     std::function<Ptr(Ptr)> g(one_arg); g(x);
     std::function<void(Ptr)> h(one_arg_void); h(x);
+    std::function<void()> i(Callable<Holder<Incomplete>>{});
     return 0;
 }
Index: libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/addressof.pass.cpp
===================================================================
--- /dev/null
+++ libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/addressof.pass.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <functional>
+
+// class function<R(ArgTypes...)>
+
+// This test runs in C++03, but we have deprecated using std::function in C++03.
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
+
+// Make sure we can use std::function with a type that has a hostile overload
+// of operator&().
+
+#include <functional>
+#include <cassert>
+
+#include "operator_hijacker.h"
+
+struct TrapAddressof : operator_hijacker {
+    int operator()() const { return 1; }
+};
+
+int main(int, char**) {
+    std::function<int()> f = TrapAddressof();
+    assert(f() == 1);
+    return 0;
+}
Index: libcxx/include/__functional/function.h
===================================================================
--- libcxx/include/__functional/function.h
+++ libcxx/include/__functional/function.h
@@ -16,6 +16,7 @@
 #include <__functional/invoke.h>
 #include <__functional/unary_function.h>
 #include <__iterator/iterator_traits.h>
+#include <__memory/addressof.h>
 #include <__memory/allocator_traits.h>
 #include <__memory/compressed_pair.h>
 #include <__memory/shared_ptr.h>
@@ -360,7 +361,7 @@
 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
 {
     if (__ti == typeid(_Fp))
-        return &__f_.__target();
+        return _VSTD::addressof(__f_.__target());
     return nullptr;
 }
 
@@ -1392,7 +1393,7 @@
 __func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
 {
     if (__ti == typeid(_Fp))
-        return &__f_.first();
+        return _VSTD::addressof(__f_.first());
     return (const void*)0;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116380.397324.patch
Type: text/x-patch
Size: 3247 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220104/79f1a29f/attachment.bin>


More information about the libcxx-commits mailing list