Understanding the compiler errors with std::bind - c++

How do I get this to compile:
#include <functional>
#include <iostream>
#include <memory>
#include <string>
inline void print(std::string string1, std::string* string2)
{
std::cout << string1 << " " << string2 << std::endl;
delete string2;
}
class class1
{
public:
std::string foo{"Hello"};
std::shared_ptr<std::string> foo2;
class1();
};
class1::class1()
{
auto boundFunc = std::bind(print, foo, std::placeholders::_2);
foo2 = std::shared_ptr<std::string>(new std::string("world"), boundFunc);
}
int main()
{
class1 test;
}
GCC gives a long and cryptic bunch of error messages:
g++ -std=c++17 -o bind bind.cpp
In file included from /usr/include/c++/7.3.1/bits/shared_ptr.h:52:0,
from /usr/include/c++/7.3.1/memory:81,
from bind.cpp:21:
/usr/include/c++/7.3.1/bits/shared_ptr_base.h: In instantiation of ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Yp*, _Deleter) [with _Yp = std::__cxx11::basic_string<char>; _Deleter = std::_Bind<void (*(std::__cxx11::basic_string<char>, std::_Placeholder<2>))(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*)>; <template-parameter-2-3> = void; _Tp = std::__cxx11::basic_string<char>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’:
/usr/include/c++/7.3.1/bits/shared_ptr.h:147:48: required from ‘std::shared_ptr<_Tp>::shared_ptr(_Yp*, _Deleter) [with _Yp = std::__cxx11::basic_string<char>; _Deleter = std::_Bind<void (*(std::__cxx11::basic_string<char>, std::_Placeholder<2>))(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*)>; <template-parameter-2-3> = void; _Tp = std::__cxx11::basic_string<char>]’
bind.cpp:41:76: required from here
/usr/include/c++/7.3.1/bits/shared_ptr_base.h:1090:4: error: static assertion failed: deleter expression d(p) is well-formed
static_assert(__is_invocable<_Deleter&, _Yp*&>::value,
^~~~~~~~~~~~~
/usr/include/c++/7.3.1/bits/shared_ptr_base.h: In instantiation of ‘std::__shared_count<_Lp>::__shared_count(_Ptr, _Deleter, _Alloc) [with _Ptr = std::__cxx11::basic_string<char>*; _Deleter = std::_Bind<void (*(std::__cxx11::basic_string<char>, std::_Placeholder<2>))(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*)>; _Alloc = std::allocator<void>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’:
/usr/include/c++/7.3.1/bits/shared_ptr_base.h:605:57: required from ‘std::__shared_count<_Lp>::__shared_count(_Ptr, _Deleter) [with _Ptr = std::__cxx11::basic_string<char>*; _Deleter = std::_Bind<void (*(std::__cxx11::basic_string<char>, std::_Placeholder<2>))(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*)>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7.3.1/bits/shared_ptr_base.h:1088:48: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Yp*, _Deleter) [with _Yp = std::__cxx11::basic_string<char>; _Deleter = std::_Bind<void (*(std::__cxx11::basic_string<char>, std::_Placeholder<2>))(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*)>; <template-parameter-2-3> = void; _Tp = std::__cxx11::basic_string<char>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7.3.1/bits/shared_ptr.h:147:48: required from ‘std::shared_ptr<_Tp>::shared_ptr(_Yp*, _Deleter) [with _Yp = std::__cxx11::basic_string<char>; _Deleter = std::_Bind<void (*(std::__cxx11::basic_string<char>, std::_Placeholder<2>))(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*)>; <template-parameter-2-3> = void; _Tp = std::__cxx11::basic_string<char>]’
bind.cpp:41:76: required from here
/usr/include/c++/7.3.1/bits/shared_ptr_base.h:623:11: error: no match for call to ‘(std::_Bind<void (*(std::__cxx11::basic_string<char>, std::_Placeholder<2>))(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*)>) (std::__cxx11::basic_string<char>*&)’
__d(__p); // Call _Deleter on __p.
~~~^~~~~
In file included from bind.cpp:19:0:
/usr/include/c++/7.3.1/functional:547:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*); _Bound_args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Placeholder<2>}]
operator()(_Args&&... __args)
^~~~~~~~
/usr/include/c++/7.3.1/functional:547:2: note: template argument deduction/substitution failed:
/usr/include/c++/7.3.1/functional:558:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*); _Bound_args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Placeholder<2>}]
operator()(_Args&&... __args) const
^~~~~~~~
/usr/include/c++/7.3.1/functional:558:2: note: template argument deduction/substitution failed:
/usr/include/c++/7.3.1/functional:576:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*); _Bound_args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Placeholder<2>}]
operator()(_Args&&... __args) volatile
^~~~~~~~
/usr/include/c++/7.3.1/functional:576:2: note: template argument deduction/substitution failed:
/usr/include/c++/7.3.1/functional:588:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*); _Bound_args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Placeholder<2>}]
operator()(_Args&&... __args) const volatile
^~~~~~~~
/usr/include/c++/7.3.1/functional:588:2: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7.3.1/bits/shared_ptr.h:52:0,
from /usr/include/c++/7.3.1/memory:81,
from bind.cpp:21:
/usr/include/c++/7.3.1/bits/shared_ptr_base.h: In instantiation of ‘void std::_Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp>::_M_dispose() [with _Ptr = std::__cxx11::basic_string<char>*; _Deleter = std::_Bind<void (*(std::__cxx11::basic_string<char>, std::_Placeholder<2>))(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*)>; _Alloc = std::allocator<void>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’:
bind.cpp:47:1: required from here
/usr/include/c++/7.3.1/bits/shared_ptr_base.h:470:25: error: no match for call to ‘(std::_Bind<void (*(std::__cxx11::basic_string<char>, std::_Placeholder<2>))(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*)>) (std::__cxx11::basic_string<char>*&)’
{ _M_impl._M_del()(_M_impl._M_ptr); }
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
In file included from bind.cpp:19:0:
/usr/include/c++/7.3.1/functional:547:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*); _Bound_args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Placeholder<2>}]
operator()(_Args&&... __args)
^~~~~~~~
/usr/include/c++/7.3.1/functional:547:2: note: template argument deduction/substitution failed:
/usr/include/c++/7.3.1/functional:558:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*); _Bound_args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Placeholder<2>}]
operator()(_Args&&... __args) const
^~~~~~~~
/usr/include/c++/7.3.1/functional:558:2: note: template argument deduction/substitution failed:
/usr/include/c++/7.3.1/functional:576:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*); _Bound_args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Placeholder<2>}]
operator()(_Args&&... __args) volatile
^~~~~~~~
/usr/include/c++/7.3.1/functional:576:2: note: template argument deduction/substitution failed:
/usr/include/c++/7.3.1/functional:588:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>*); _Bound_args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Placeholder<2>}]
operator()(_Args&&... __args) const volatile
^~~~~~~~
/usr/include/c++/7.3.1/functional:588:2: note: template argument deduction/substitution failed:
I'm not sure what to make of this output, and I haven't found any good documentation of std::bind which provides examples for all the various ways in which it might be used, only the most basic cases.
stackoverflow wants more details, but I don't even know what else to provide. It should be obvious from the code what the problem is. If I knew what to say I would be able to search for it already, but I'm not sure how to describe this problem.

Simple fix: Replace
auto boundFunc = std::bind(print, foo, std::placeholders::_2);
with
auto boundFunc = std::bind(print, foo, std::placeholders::_1);
Why?
In std::bind, the placeholders indicate the index of the argument to the result function. std::placeholders::_1 means "use the first argument passed to me in this position".
When you pass std::placeholders::_2, your boundFunc will take the wrong number of arguments (2 instead of 1) and no longer be a valid deleter for an std::shared_ptr.

The error is subtle but there. You probably used std::placeholders::_2 because you are using the second argument, right? Well, that's not how you use them.
The number _N designates that the argument number N (when you call the result) is bound to _N. In other words, you need to enumerate them from 1 onwards in your case:
auto boundFunc = std::bind(print, foo, std::placeholders::_1);
// ^

Related

Using std::allocate_shared with polymorphic resource allocators

I am attempting to create shared pointers with a std::pmr::monotonic_buffer_resource, and I cannot make it compile. What am I missing?
https://godbolt.org/z/R9jdju
#include <memory>
#include <memory_resource>
int main() {
char buffer[100];
std::pmr::monotonic_buffer_resource mbr(buffer, 100);
std::shared_ptr<double> sp = std::allocate_shared<double>(mbr);
}
In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ext/alloc_traits.h:34,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/stl_uninitialized.h:67,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/memory:66,
from <source>:1:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/alloc_traits.h: In substitution of 'template<class _Alloc, class _Up> using __alloc_rebind = typename std::__allocator_traits_base::__rebind<_Alloc, _Up>::type [with _Alloc = std::pmr::monotonic_buffer_resource; _Up = std::_Sp_counted_ptr_inplace<double, std::pmr::monotonic_buffer_resource, __gnu_cxx::_S_atomic>]':
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:542:13: required from 'class std::_Sp_counted_ptr_inplace<double, std::pmr::monotonic_buffer_resource, __gnu_cxx::_S_atomic>'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:679:43: required from 'std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:1371:71: required from 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; _Tp = double; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:408:59: required from 'std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; _Tp = double]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:859:14: required from 'std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}]'
<source>:7:66: required from here
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/alloc_traits.h:78:11: error: no type named 'type' in 'struct std::__allocator_traits_base::__rebind<std::pmr::monotonic_buffer_resource, std::_Sp_counted_ptr_inplace<double, std::pmr::monotonic_buffer_resource, __gnu_cxx::_S_atomic>, void>'
78 | using __alloc_rebind
| ^~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:52,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/memory:84,
from <source>:1:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h: In instantiation of 'std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]':
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:1371:71: required from 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; _Tp = double; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:408:59: required from 'std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; _Tp = double]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:859:14: required from 'std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}]'
<source>:7:66: required from here
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:682:16: error: use of deleted function 'std::pmr::monotonic_buffer_resource::monotonic_buffer_resource(const std::pmr::monotonic_buffer_resource&)'
682 | auto __pi = ::new (__mem)
| ^~~~~~~~~~~~~
683 | _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from <source>:2:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/memory_resource:604:5: note: declared here
604 | monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
| ^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:52,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/memory:84,
from <source>:1:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:546:33: note: initializing argument 1 of 'std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {}; _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]'
546 | _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
| ~~~~~~~^~~
That long error message basically boils down to these 2 errors:
error: no type named 'type' in 'struct std::__allocator_traits_base::__rebind<std::pmr::monotonic_buffer_resource, std::_Sp_counted_ptr_inplace<double, std::pmr::monotonic_buffer_resource, __gnu_cxx::_S_atomic>, void>'
error: use of deleted function 'std::pmr::monotonic_buffer_resource::monotonic_buffer_resource(const std::pmr::monotonic_buffer_resource&)'
std::pmr::monotonic_buffer_resource does not satisfy the requirements that std::allocate_shared() expects, specifically:
All memory allocation is done using a copy of alloc, which must satisfy the Allocator requirements.
In particular, "a COPY of alloc", which fails since monotonic_buffer_resource's copy constructor is delete'd so it can't be copied.
As #MilesBudnek stated in comments, you can wrap the monotonic_buffer_resource inside a std::pmr::polymorphic_allocator, which is designed to be used as an Allocator for standard containers:
The class template std::pmr::polymorphic_allocator is an Allocator which exhibits different allocation behavior depending upon the std::pmr::memory_resource from which it is constructed.
For example:
#include <memory>
#include <memory_resource>
int main() {
char buffer[100];
std::pmr::monotonic_buffer_resource mbr(buffer, 100);
auto sp = std::allocate_shared<double, std::pmr::polymorphic_allocator<double>>(&mbr);
}
https://godbolt.org/z/-xFfFY
Alternatively:
#include <memory>
#include <memory_resource>
int main() {
char buffer[100];
std::pmr::monotonic_buffer_resource mbr(buffer, 100);
std::pmr::polymorphic_allocator<double> alloc(&mbr);
auto sp = std::allocate_shared<double>(alloc);
}
https://godbolt.org/z/GLE4-5

Derive from shared_ptr<T>

I have a strange compilation problem that I can't understand.
//I know, you should never derive from the STL Library
template<class T>
class SharedClass : private shared_ptr<T>
{
public:
template<class T2>
SharedClass(T2&& t2) :
shared_ptr<T>(make_shared<T>(move(t2)))
{}
virtual ~SharedClass()
{}
};
class A
{
public:
typedef function<void(SharedClass<A>)> Callback;
A(Callback callback)
{ }
};
main.cpp
SharedClass<A> shared([](SharedClass<A>){ });
Compile Log:
In file included from /usr/include/x86_64-linux-gnu/c++/7/bits/c++allocator.h:33:0,
from /usr/include/c++/7/bits/allocator.h:46,
from /usr/include/c++/7/string:41,
from /usr/include/c++/7/bits/locale_classes.h:40,
from /usr/include/c++/7/bits/ios_base.h:41,
from /usr/include/c++/7/ios:42,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from main.cpp:9:
/usr/include/c++/7/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = A; _Args = {SharedClass<A>}; _Tp = A]’:
/usr/include/c++/7/bits/alloc_traits.h:475:4: required from ‘static void std::allocator_traits<std::allocator<_CharT>>::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = A; _Args = {SharedClass<A>}; _Tp = A; std::allocator_traits<std::allocator<_CharT>>::allocator_type = std::allocator<A>]’
/usr/include/c++/7/bits/shared_ptr_base.h:526:39: required from ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {SharedClass<A>}; _Tp = A; _Alloc = std::allocator<A>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’ /usr/include/c++/7
/bits/shared_ptr_base.h:637:4: required from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = A; _Alloc = std::allocator<A>; _Args = {SharedClass<A>}; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr_base.h:1295:35: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<A>; _Args = {SharedClass<A>}; _Tp = A; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr.h:344:64: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<A>; _Args = {SharedClass<A>}; _Tp = A]’
/usr/include/c++/7/bits/shared_ptr.h:690:14: [ skipping 9 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/7/bits/shared_ptr_base.h:1295:35: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<A>; _Args = {main()::<lambda(SharedClass<A>)>}; _Tp = A; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr.h:344:64: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<A>; _Args = {main()::<lambda(SharedClass<A>)>}; _Tp = A]’ /usr/include/c++/7/bits/shared_ptr.h:690:14: required from ‘std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = A; _Alloc = std::allocator<A>; _Args = {main()::<lambda(SharedClass<A>)>}]’
/usr/include/c++/7/bits/shared_ptr.h:706:39: required from ‘std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = A; _Args = {main()::<lambda(SharedClass<A>)>}]’
main.cpp:21:33: required from ‘SharedClass<T>::SharedClass(T2&&) [with T2 = main()::<lambda(SharedClass<A>)>; T = A]’
main.cpp:39:48: required from here
/usr/include/c++/7/ext/new_allocator.h:136:4: error: no matching function for call to ‘A::A(SharedClass)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:33:5: note: candidate: A::A(A::Callback)
A(Callback callback)
^ main.cpp:33:5: note: no known conversion for argument 1 from ‘SharedClass’ to ‘A::Callback {aka std::function)>}’
main.cpp:28:7: note: candidate: constexpr A::A(const A&)
class A
^ main.cpp:28:7: note: no known conversion for argument 1 from ‘SharedClass’ to ‘const A&’
main.cpp:28:7: note: candidate: constexpr A::A(A&&)
main.cpp:28:7: note: no known conversion for argument 1 from ‘SharedClass’ to ‘A&&’
If I change the constructor of the SharedClass class like this SharedClass (T2 t2) it compiles.
As your lambda takes its parameters by value it needs to be able to create copies of those parameters, it does this by calling the move constructor. As you have defined a templated constructor with the same signature as the move constructor the compiler attempts to use it as a move constructor. Adding default copy (for completeness) and move constructors fixes the issue:
SharedClass(const SharedClass&) = default;
SharedClass(SharedClass&&) = default;
Your existing constructor doesn't work as a move constructor because there is no constructor for A which takes a SharedClass<A> parameter so make_shared<A> doesn't compile when passed a SharedClass<A> parameter.

Strange error from move and copy constructors that have shared_ptrs

I'm getting the following errors when trying compile my HuffmanNode class for an assignment, it seems to be something to do with the shared_ptrs in the class, but I have no idea as to what is actually going wrong because of the sheer amount of text that I can't seem to find the actual problem.
g++ -std=c++11 -c src/HuffmanNode.cpp -o obj/HuffmanNode.o -I headers
In file included from /usr/include/x86_64-linux-gnu/c++/7/bits/c++allocator.h:33:0,
from /usr/include/c++/7/bits/allocator.h:46,
from /usr/include/c++/7/memory:63,
from headers/HuffmanNode.hpp:9,
from src/HuffmanNode.cpp:5:
/usr/include/c++/7/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = FLNRHE001::HuffmanNode; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode]’:
/usr/include/c++/7/bits/alloc_traits.h:475:4: required from ‘static void std::allocator_traits<std::allocator<_Tp1> >::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = FLNRHE001::HuffmanNode; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocator<FLNRHE001::HuffmanNode>]’
/usr/include/c++/7/bits/shared_ptr_base.h:526:39: required from ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode; _Alloc = std::allocator<FLNRHE001::HuffmanNode>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr_base.h:637:4: required from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = FLNRHE001::HuffmanNode; _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr_base.h:1295:35: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr.h:344:64: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode]’
/usr/include/c++/7/bits/shared_ptr.h:690:14: required from ‘std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = FLNRHE001::HuffmanNode; _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}]’
/usr/include/c++/7/bits/shared_ptr.h:706:39: required from ‘std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = FLNRHE001::HuffmanNode; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}]’
src/HuffmanNode.cpp:34:72: required from here
/usr/include/c++/7/ext/new_allocator.h:136:4: error: no matching function for call to ‘FLNRHE001::HuffmanNode::HuffmanNode(const std::shared_ptr<FLNRHE001::HuffmanNode>&)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/HuffmanNode.cpp:41:1: note: candidate: FLNRHE001::HuffmanNode::HuffmanNode(FLNRHE001::HuffmanNode&&)
HuffmanNode::HuffmanNode(HuffmanNode &&n)
^~~~~~~~~~~
src/HuffmanNode.cpp:41:1: note: no known conversion for argument 1 from ‘const std::shared_ptr<FLNRHE001::HuffmanNode>’ to ‘FLNRHE001::HuffmanNode&&’
src/HuffmanNode.cpp:33:1: note: candidate: FLNRHE001::HuffmanNode::HuffmanNode(const FLNRHE001::HuffmanNode&)
HuffmanNode::HuffmanNode(const HuffmanNode &n)
^~~~~~~~~~~
src/HuffmanNode.cpp:33:1: note: no known conversion for argument 1 from ‘const std::shared_ptr<FLNRHE001::HuffmanNode>’ to ‘const FLNRHE001::HuffmanNode&’
src/HuffmanNode.cpp:11:1: note: candidate: FLNRHE001::HuffmanNode::HuffmanNode(char, int, std::shared_ptr<FLNRHE001::HuffmanNode>, std::shared_ptr<FLNRHE001::HuffmanNode>)
HuffmanNode::HuffmanNode(char c, int f, std::shared_ptr<HuffmanNode> l, std::shared_ptr<HuffmanNode> r)
^~~~~~~~~~~
src/HuffmanNode.cpp:11:1: note: candidate expects 4 arguments, 1 provided
In file included from /usr/include/x86_64-linux-gnu/c++/7/bits/c++allocator.h:33:0,
from /usr/include/c++/7/bits/allocator.h:46,
from /usr/include/c++/7/memory:63,
from headers/HuffmanNode.hpp:9,
from src/HuffmanNode.cpp:5:
/usr/include/c++/7/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = FLNRHE001::HuffmanNode; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp
= FLNRHE001::HuffmanNode]’:
/usr/include/c++/7/bits/alloc_traits.h:475:4: required from ‘static void std::allocator_traits<std::allocator<_Tp1> >::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = FLNRHE001::HuffmanNode; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocator<FLNRHE001::HuffmanNode>]’
/usr/include/c++/7/bits/shared_ptr_base.h:526:39: required from ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&};
_Tp = FLNRHE001::HuffmanNode; _Alloc = std::allocator<FLNRHE001::HuffmanNode>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr_base.h:637:4: required from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = FLNRHE001::HuffmanNode; _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr_base.h:1295:35: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr.h:344:64: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode]’
/usr/include/c++/7/bits/shared_ptr.h:690:14: required from ‘std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = FLNRHE001::HuffmanNode; _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}]’
/usr/include/c++/7/bits/shared_ptr.h:706:39: required from ‘std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = FLNRHE001::HuffmanNode; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}]’
src/HuffmanNode.cpp:93:52: required from here
/usr/include/c++/7/ext/new_allocator.h:136:4: error: no matching function for call to ‘FLNRHE001::HuffmanNode::HuffmanNode(std::shared_ptr<FLNRHE001::HuffmanNode>&)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/HuffmanNode.cpp:41:1: note: candidate: FLNRHE001::HuffmanNode::HuffmanNode(FLNRHE001::HuffmanNode&&)
HuffmanNode::HuffmanNode(HuffmanNode &&n)
^~~~~~~~~~~
src/HuffmanNode.cpp:41:1: note: no known conversion for argument 1 from ‘std::shared_ptr<FLNRHE001::HuffmanNode>’ to ‘FLNRHE001::HuffmanNode&&’
src/HuffmanNode.cpp:33:1: note: candidate: FLNRHE001::HuffmanNode::HuffmanNode(const FLNRHE001::HuffmanNode&)
HuffmanNode::HuffmanNode(const HuffmanNode &n)
^~~~~~~~~~~
src/HuffmanNode.cpp:33:1: note: no known conversion for argument 1 from ‘std::shared_ptr<FLNRHE001::HuffmanNode>’ to ‘const FLNRHE001::HuffmanNode&’
src/HuffmanNode.cpp:11:1: note: candidate: FLNRHE001::HuffmanNode::HuffmanNode(char, int, std::shared_ptr<FLNRHE001::HuffmanNode>, std::shared_ptr<FLNRHE001::HuffmanNode>)
HuffmanNode::HuffmanNode(char c, int f, std::shared_ptr<HuffmanNode> l, std::shared_ptr<HuffmanNode> r)
^~~~~~~~~~~
src/HuffmanNode.cpp:11:1: note: candidate expects 4 arguments, 1 provided
I've tried looking up the errors but can't seem to find anything that solves the problem.
I can't post my entire code due to it being an assignment, but the lines that are mentioned in the error text are my constructor declarations, and as stated above, the problem seems to lie in the shared_ptrs:
line 11: HuffmanNode::HuffmanNode(char c, int f, std::shared_ptr<HuffmanNode> l, std::shared_ptr<HuffmanNode> r)
line 33: HuffmanNode::HuffmanNode(const HuffmanNode &n) : ch(n.ch), freq(n.freq), left(std::make_shared<HuffmanNode>(n.left)), right(std::make_shared<HuffmanNode>(n.right))
line 41: HuffmanNode::HuffmanNode(HuffmanNode &&n) : ch(std::move(n.ch)), freq(std::move(n.freq)), left(std::move(n.left)), right(std::move(n.right))
EDIT:
Here's what I can post according to the school regulations, the only method that has any content inside it of the problem areas is the move constructor, and that is just ensuring that there will be no double deletion errors when the destructor is called. This gives me the same errors as above does.
#include <memory>
class HuffmanNode
{
char ch;
int freq;
std::shared_ptr<HuffmanNode> left, right;
// Node constructor for leaf node
HuffmanNode(char c, int f, std::shared_ptr<HuffmanNode> l, std::shared_ptr<HuffmanNode> r)
: ch(c), freq(f), left(l), right(r)
{
// Handled in initialiser list
}
HuffmanNode(const HuffmanNode &n)
: ch(n.ch), freq(n.freq), left(std::make_shared<HuffmanNode>(n.left)),
right(std::make_shared<HuffmanNode>(n.right))
{
// Handled by initialiser list
}
// Move Constructor
HuffmanNode(HuffmanNode &&n)
: ch(std::move(n.ch)), freq(std::move(n.freq)),
left(std::move(n.left)),
right(std::move(n.right))
{
}
};
The errors were arising from attempting to use make_shared to create a copy of the left and right pointers, in this case I only require a shallow copy as is made by the shared_ptr copy constructor.
The corrected solution was only a change to the move constructor as follows-:
HuffmanNode(const HuffmanNode &n)
: ch(n.ch), freq(n.freq), left(n.left),
right(n.right)
{
// Handled by initialiser list
}
Thanks to Sam Varshavchik for the help in this!

Compilation Error when using tr1::function

The purpose is to execute CVS890Executor::do_full_frame when calling the m_callback_fn within CDevVS890.
Following is the incriminated code:
"CDevVS890.h"
typedef std::tr1::function<void (void* frame, int len)> DoFrameFn;
class CDevVS890
{
public:
CDevVS890();
void receive();
DoFrameFn m_callback_fn;
}
"CDevVS890.cpp"
void CDevVS890::receive()
{
...
m_callback_fn((void*)frame, (int)len);
}
/*----------------------------------------------------------------------*/
"CVS890Executor.h"
class CVS890Executor
{
public:
CVS890Executor();
private:
void hookup_to_DevVS890();
void do_full_frame( void* frame, int len );
}
"CVS890Executor.cpp"
CVS890Executor::CVS890Executor()
{
hookup_to_DevVS890();
}
void CVS890Executor::hookup_to_DevVS890()
{
m_pDevVS890 = new CDevVS890();
m_pDevVS890->m_callback_fn =
std::tr1::bind(&CVS890Executor::do_full_frame, this, _1);
}
void CVS890Executor::do_full_frame(void* frame, int len)
{
...
}
The errors are multiple and very difficult to read:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1/functional:56,
from ../../src/Common/CDevVS890.h:17,
from CVS890Executor.h:13,
from CVS890Executor.cpp:8:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional: In member function âtypename std::tr1::result_of<_Functor(typename std::tr1::result_of 0)>(_Bound_args, std::tr1::tuple<_UElements ...>)>::type ...)>::type std::tr1::_Bind<_Functor(_Bound_args ...)>::__call(const std::tr1::tuple<_UElements ...>&, std::tr1::_Index_tuple<_Indexes ...>) [with _Args = void*&, int&, int ..._Indexes = 0, 1, _Functor = std::tr1::_Mem_fn, _Bound_args = CVS890Executor*, std::tr1::_Placeholder<1>]â:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1191: instantiated from âtypename std::tr1::result_of<_Functor(typename std::tr1::result_of 0)>(_Bound_args, std::tr1::tuple<_UElements ...>)>::type ...)>::type std::tr1::_Bind<_Functor(_Bound_args ...)>::operator()(_Args& ...) [with _Args = void*, int, _Functor = std::tr1::_Mem_fn, _Bound_args = CVS890Executor*, std::tr1::_Placeholder<1>]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1668: instantiated from âstatic void std::tr1::_Function_handler::_M_invoke(const std::tr1::_Any_data&, _ArgTypes ...) [with _Functor = std::tr1::_Bind(CVS890Executor*, std::tr1::_Placeholder<1>)>, _ArgTypes = void*, int]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:2005: instantiated from âstd::tr1::function<_Res(_ArgTypes ...)>::function(_Functor, typename __gnu_cxx::__enable_if<(! std::tr1::is_integral::value), std::tr1::function<_Res(_ArgTypes ...)>::Useless>::_type) [with _Functor = std::tr1::_Bind(CVS890Executor*, std::tr1::_Placeholder<1>)>, _Res = void, _ArgTypes = void*, int]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1885: instantiated from âtypename __gnu_cxx::__enable_if<(! std::tr1::is_integral::value), std::tr1::function<_Res(ArgTypes ...)>&>::_type std::tr1::function<_Res(_ArgTypes ...)>::operator=(_Functor) [with _Functor = std::tr1::_Bind(CVS890Executor*, std::tr1::_Placeholder<1>)>, _Res = void, _ArgTypes = void*, int]â
CVS890Executor.cpp:115: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1137: error: no match for call to â(std::tr1::_Mem_fn) (CVS890Executor*&, void*&)â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:546: note: candidates are: _Res std::tr1::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&, _ArgTypes ...) const [with _Res = void, _Class = CVS890Executor, _ArgTypes = void*, int]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:551: note: _Res std::tr1::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class*, _ArgTypes ...) const [with _Res = void, _Class = CVS890Executor, _ArgTypes = void*, int]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/tr1_impl/functional:1137: error: return-statement with a value, in function returning 'void'
make: * [CVS890Executor.o] Error 1
Any idea what's wrong with this?
Cheers
You forgot about the second argument. Your call of bind function should be like this:
std::tr1::bind(&CVS890Executor::do_full_frame, this, _1, _2);
// ^^
In CVS890Executor::hookup_to_DevVS890(), you are not binding any arguments to the member function do_full_frame.
You are also trying to assign the return value of the function to m_callback_fn but do_full_frame() is declared to return void (no return value).

g++ std::bind error with shared_ptr

I'm having trouble understanding why the following code does not compile.
#include <memory>
#include <functional>
class Foo
{
public:
void Bar(int i) {}
};
void X(std::function<void(std::shared_ptr<Foo>)> f)
{
}
int main()
{
std::shared_ptr<Foo> f(new Foo);
auto f1(std::bind(&Foo::Bar, std::placeholders::_1, 1));
X(f1);
return 0;
}
g++ (4.6.3) outputs...
n file included from /usr/include/c++/4.6/memory:80:0,
from test.cpp:1:
/usr/include/c++/4.6/functional: In static member function ‘static void std::_Function_handler<void(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Functor = std::_Bind<std::_Mem_fn<void (Foo::*)(int)>(std::_Placeholder<1>, int)>, _ArgTypes = {std::shared_ptr<Foo>}]’:
/usr/include/c++/4.6/functional:2148:6: instantiated from ‘std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = std::_Bind<std::_Mem_fn<void (Foo::*)(int)>(std::_Placeholder<1>, int)>, _Res = void, _ArgTypes = {std::shared_ptr<Foo>}, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<void(std::shared_ptr<Foo>)>::_Useless]’
test.cpp:19:7: instantiated from here
/usr/include/c++/4.6/functional:1778:2: error: no match for call to ‘(std::_Bind<std::_Mem_fn<void (Foo::*)(int)>(std::_Placeholder<1>, int)>) (std::shared_ptr<Foo>)’
/usr/include/c++/4.6/functional:1130:11: note: candidates are:
/usr/include/c++/4.6/functional:1201:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}, _Result = _Result, _Functor = std::_Mem_fn<void (Foo::*)(int)>, _Bound_args = {std::_Placeholder<1>, int}]
/usr/include/c++/4.6/functional:1215:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}, _Result = _Result, _Functor = std::_Mem_fn<void (Foo::*)(int)>, _Bound_args = {std::_Placeholder<1>, int}]
/usr/include/c++/4.6/functional:1229:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}, _Result = _Result, _Functor = std::_Mem_fn<void (Foo::*)(int)>, _Bound_args = {std::_Placeholder<1>, int}]
/usr/include/c++/4.6/functional:1243:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}, _Result = _Result, _Functor = std::_Mem_fn<void (Foo::*)(int)>, _Bound_args = {std::_Placeholder<1>, int}]
It's a GCC bug, which I fixed in 4.7, but I don't remember exactly which bug. I'll try to figure it out ...
Edit: Aha, it's PR 55463 so isn't fixed in 4.7, it's only fixed on GCC trunk (what will be 4.8)
The bug was that the call wrapper returned by mem_fn doesn't accept rvalues, and your std::function<void(std::shared_ptr<Foo>) type passes an rvalue shared_ptr<Foo> to the call wrapper return by bind.
As a workaround you can change your function signature to this:
void X(std::function<void(const std::shared_ptr<Foo>&)> f)
I don't think I can backport the fix to the 4.7 branch, because as a result of that bug I made some quite large changes to mem_fn that aren't really suitable for a stable release branch (I also found a new defect in the standard)