The following code fails to compile using GCC with libstdc++ and using Clang with libc++:
#include <functional>
template<class F> class X {
public:
static void foo(F f, int)
{
f();
}
};
template<class F> void bar(F f)
{
std::bind(&X<F>::foo, f, 2)();
}
void baz(int) {}
int main()
{
bar(std::bind(&baz, 1));
}
As far as I understand std::bind()
, this is supposed to be valid. Any ideas why it fails?
Here is the output from GCC 4.9:
t.cpp: In instantiation of ‘void bar(F) [with F = std::_Bind]’: t.cpp:20:26: required from here t.cpp:13:31: error: no match for call to ‘(std::_Bind_helper, int), std::_Bind&, int>::type {aka std::_Bind, int))(std::_Bind, int)>}) ()’ std::bind(&X::foo, f, 2)(); ^ In file included from t.cpp:1:0: /usr/include/c++/4.9/functional:1248:11: note: candidates are: class _Bind ^ /usr/include/c++/4.9/functional:1319:2: note: template _Result std::_Bind::operator()(_Args&& ...) [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(std::_Bind, int); _Bound_args = {std::_Bind, int}] operator()(_Args&&... __args) ^ /usr/include/c++/4.9/functional:1319:2: note: template argument deduction/substitution failed: /usr/include/c++/4.9/functional:1317:40: error: could not convert ‘std::_Mu, true, false>().std::_Mu::operator(), {}>((* & std::declval&>()), (* & std::declval&>()))’ from ‘void’ to ‘std::_Bind’ std::declval&>() )... ) )> ^ /usr/include/c++/4.9/functional:1333:2: note: template _Result std::_Bind::operator()(_Args&& ...) const [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(std::_Bind, int); _Bound_args = {std::_Bind, int}] operator()(_Args&&... __args) const ^ /usr/include/c++/4.9/functional:1333:2: note: template argument deduction/substitution failed: /usr/include/c++/4.9/functional:1331:40: error: could not convert ‘std::_Mu, true, false>().std::_Mu::operator(), {}>((* & std::declval&>()), (* & std::declval&>()))’ from ‘void’ to ‘std::_Bind’ std::declval&>() )... ) )> ^ /usr/include/c++/4.9/functional:1347:2: note: template _Result std::_Bind::operator()(_Args&& ...) volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(std::_Bind, int); _Bound_args = {std::_Bind, int}] operator()(_Args&&... __args) volatile ^ /usr/include/c++/4.9/functional:1347:2: note: template argument deduction/substitution failed: /usr/include/c++/4.9/functional:1345:40: error: could not convert ‘std::_Mu, true, false>().std::_Mu::operator(), {}>((* & std::declval&>()), (* & std::declval&>()))’ from ‘void’ to ‘std::_Bind’ std::declval&>() )... ) )> ^ /usr/include/c++/4.9/functional:1361:2: note: template _Result std::_Bind::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = void (*)(std::_Bind, int); _Bound_args = {std::_Bind, int}] operator()(_Args&&... __args) const volatile ^ /usr/include/c++/4.9/functional:1361:2: note: template argument deduction/substitution failed: /usr/include/c++/4.9/functional:1359:40: error: could not convert ‘std::_Mu, true, false>().std::_Mu::operator(), {}>((* & std::declval&>()), (* & std::declval&>()))’ from ‘void’ to ‘std::_Bind’ std::declval&>() )... ) )>