Skip to content

shared_ptr of function type #27940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
K-ballo opened this issue Apr 29, 2016 · 2 comments
Closed

shared_ptr of function type #27940

K-ballo opened this issue Apr 29, 2016 · 2 comments
Labels
bugzilla Issues migrated from bugzilla libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Comments

@K-ballo
Copy link

K-ballo commented Apr 29, 2016

Bugzilla Link 27566
Resolution FIXED
Resolved on May 25, 2017 08:43
Version unspecified
OS All
CC @AlisdairM,@mclow

Extended Description

The following snippet triggers a compilation error:

#include <memory>

struct Foo {};
using FooFn = Foo ();
using FooFnPtr = Foo (*)();
using FooDel = void (*)(FooFnPtr);
static_assert(std::is_same<FooFn*, FooFnPtr>::value, "");

void del(FooFnPtr) {}
Foo fun() { return {}; }

int main() {
    std::shared_ptr<FooFn>(&fun, &del);
}

The error happens when std::shared_ptr<Foo ()> attempts to instantiate std::allocator<Foo ()> in a constructor that does not need that allocator:

In file included from prog.cc:1:
/usr/local/libcxx-head/include/c++/v1/memory:1726:45: error: multiple overloads of 'address' instantiate to the same signature 'const_pointer (const_reference) const noexcept' (aka 'Foo ((Foo (&)()) const noexcept)()')
_LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
^
/usr/local/libcxx-head/include/c++/v1/memory:4185:44: note: in instantiation of template class 'std::__1::allocator<Foo ()>' requested here
_cntrl = new _CntrlBlk(__p, __d, allocator<_Yp>());
^
prog.cc:13:5: note: in instantiation of function template specialization 'std::__1::shared_ptr<Foo ()>::shared_ptr<Foo (), void (
)(Foo (*)())>' requested here
std::shared_ptr(&fun, &del);
^

The original use case leverages std::shared_ptr to bundle a dynamically loaded function together with a deleter that unloads the dynamic library when the function is no longer referenced. It is unclear whether this use case is supported, but I did not spot anything in the standard that would forbid it.

@llvmbot
Copy link
Member

llvmbot commented Mar 10, 2017

I submitted a patch up for this here: https://reviews.llvm.org/D30837
Thanks for the report!

@llvmbot
Copy link
Member

llvmbot commented May 25, 2017

Fixed in r303874.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

No branches or pull requests

2 participants