Easy: Mark folly::static_function_deleter::operator() const
authorFelix Handte <felixh@fb.com>
Thu, 25 Aug 2016 00:42:37 +0000 (17:42 -0700)
committerFacebook Github Bot 8 <facebook-github-bot-8-bot@fb.com>
Thu, 25 Aug 2016 00:53:29 +0000 (17:53 -0700)
Summary:
First, folly::static_function_deleter::operator() is in fact a const
operation, so this should be a reasonable change. The motivation for this is
to make folly::ThreadLocalPtr and folly::static_function_deleter play nice with
each other.

Minimal example:
```lang=c++

void deleter(int* ptr) {
  free(ptr);
}

int main(int argc, char* argv[]) {
  folly::ThreadLocalPtr<int> tl;
  tl.reset(std::unique_ptr<int, folly::static_function_deleter<int, deleter>>(
      new int(0)));
  return 0;
}
```

Currently produces:
```
folly/ThreadLocal.h:207:7: error: no matching function for call to object of type 'const folly::static_function_deleter<int, &deleter>'
      delegate(ptr);
      ^~~~~~~~
Test.cpp:10:6: note: in instantiation of function template specialization 'folly::ThreadLocalPtr<int, void>::reset<int, folly::static_function_deleter<int, &deleter>, void>' requested here
  tl.reset(std::unique_ptr<int, folly::static_function_deleter<int, deleter>>(new int(0)));
     ^
folly/Memory.h:91:8: note: candidate function not viable: 'this' argument has type 'const folly::static_function_deleter<int, &deleter>', but method is not marked const
  void operator()(T* t) { f(t); }
       ^
1 error generated.
```

With the fix, the build succeeds.

Reviewed By: yfeldblum

Differential Revision: D3764624

fbshipit-source-id: c28c791b79f1415704c205c36bfda2d888d6c010

folly/Memory.h

index 81ac176cb11a3494923ff5f378844472f0f1f926..62490f90b80abf2f1ee8c1d842880d636b4e20cc 100644 (file)
@@ -88,7 +88,9 @@ make_unique(Args&&...) = delete;
 
 template <typename T, void(*f)(T*)>
 struct static_function_deleter {
-  void operator()(T* t) { f(t); }
+  void operator()(T* t) const {
+    f(t);
+  }
 };
 
 /**