I'm trying to use std::bind
to call std::reference_wrapper::get
but I can't get it to compile. I'm sure I'm overlooking something obvious but the compiler errors are not helping me out. This code is contrived and doesn't represent my actual use case:
#include <functional>
#include <iostream>
struct A
{
void p() {std::cout << this << '\n';};
};
using ARef = std::reference_wrapper< A >;
int main()
{
A a;
a.p();
auto p = std::bind (&A::p, std::placeholders::_1);
p (a); // ok
ARef ar (a);
p (ar.get()); // ok
auto get = std::bind (&ARef::get, std::placeholders::_1);
p (get (ar)); // error
}
clang output:
main.cpp:21:12: error: no matching function for call to object of type 'std::_Bind::*)() const> (std::_Placeholder<1>)>' p (get (ar)); // error ^~~ /usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.3.0/../../../../include/c++/6.3.0/functional:989:2: note: candidate template ignored: substitution failure [with _Args = &>]: no matching function for call to object of type 'std::_Mem_fn::*)() const>' operator()(_Args&&... __args) ^ /usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.3.0/../../../../include/c++/6.3.0/functional:1003:2: note: candidate template ignored: substitution failure [with _Args = &>]: no matching function for call to object of type 'const std::_Mem_fn::*)() const>' operator()(_Args&&... __args) const ^ /usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.3.0/../../../../include/c++/6.3.0/functional:1017:2: note: candidate template ignored: substitution failure [with _Args = &>]: no matching function for call to object of type 'volatile std::_Mem_fn::*)() const>' operator()(_Args&&... __args) volatile ^ /usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.3.0/../../../../include/c++/6.3.0/functional:1031:2: note: candidate template ignored: substitution failure [with _Args = &>]: no matching function for call to object of type 'const volatile std::_Mem_fn::*)() const>' operator()(_Args&&... __args) const volatile ^ 1 error generated.
gcc output:
main.cpp: In function 'int main()': main.cpp:21:19: error: no match for call to '(std::_Bind::*)() const noexcept>(std::_Placeholder<1>)>) (ARef&)' p (get (ar)); // error ^ In file included from main.cpp:1:0: /usr/local/include/c++/6.3.0/functional:989:2: note: candidate: template _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}; _Result = _Result; _Functor = std::_Mem_fn::*)() const noexcept>; _Bound_args = {std::_Placeholder<1>}] operator()(_Args&&... __args) ^~~~~~~~ /usr/local/include/c++/6.3.0/functional:989:2: note: template argument deduction/substitution failed: /usr/local/include/c++/6.3.0/functional:985:39: error: no match for call to '(std::_Mem_fn::*)() const noexcept>) (std::reference_wrapper&)' = decltype( std::declval<_Functor&>()( ~~~~~~~~~~~~~~~~~~~~~~~~~^ _Mu<_Bound_args>()( std::declval<_Bound_args&>(), ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ std::declval&>() )... ) )> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/include/c++/6.3.0/functional:600:2: note: candidate: template decltype (std::__invoke(((const std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>)this)->std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::_M_pmf, (forward<_Args>)(std::_Mem_fn_base::operator()::__args)...)) std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _MemFunPtr = A& (std::reference_wrapper::)() const noexcept; bool __is_mem_fn = true] operator()(_Args&&... __args) const ^~~~~~~~ /usr/local/include/c++/6.3.0/functional:600:2: note: template argument deduction/substitution failed: /usr/local/include/c++/6.3.0/functional: In substitution of 'template decltype (std::__invoke(((const std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>)this)->std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::_M_pmf, (forward<_Args>)(std::_Mem_fn_base::operator()::__args)...)) std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::operator()(_Args&& ...) const [with _Args = {std::reference_wrapper&}]': /usr/local/include/c++/6.3.0/functional:985:39: required from here /usr/local/include/c++/6.3.0/functional:603:27: error: no matching function for call to '__invoke(A& (std::reference_wrapper:: const&)() const noexcept, std::reference_wrapper&)' -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...)) ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/include/c++/6.3.0/functional:245:5: note: candidate: template typename std::result_of<_Callable&&(_Args&& ...)>::type std::__invoke(_Callable&&, _Args&& ...) __invoke(_Callable&& __fn, _Args&&... __args) ^~~~~~~~ /usr/local/include/c++/6.3.0/functional:245:5: note: template argument deduction/substitution failed: /usr/local/include/c++/6.3.0/functional: In substitution of 'template typename std::result_of<_Callable&&(_Args&& ...)>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = A& (std::reference_wrapper::* const&)() const noexcept; _Args = {std::reference_wrapper&}]': /usr/local/include/c++/6.3.0/functional:603:27: required by substitution of 'template decltype (std::__invoke(((const std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>)this)->std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::_M_pmf, (forward<_Args>)(std::_Mem_fn_base::operator()::__args)...)) std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::operator()(_Args&& ...) const [with _Args = {std::reference_wrapper&}]' /usr/local/include/c++/6.3.0/functional:985:39: required from here /usr/local/include/c++/6.3.0/functional:245:5: error: no type named 'type' in 'class std::result_of::* const&(std::reference_wrapper&))() const noexcept>' /usr/local/include/c++/6.3.0/functional:1003:2: note: candidate: template _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _Result = _Result; _Functor = std::_Mem_fn::*)() const noexcept>; _Bound_args = {std::_Placeholder<1>}] operator()(_Args&&... __args) const ^~~~~~~~ /usr/local/include/c++/6.3.0/functional:1003:2: note: template argument deduction/substitution failed: /usr/local/include/c++/6.3.0/functional:999:53: error: no match for call to '(const std::_Mem_fn::*)() const noexcept>) (std::reference_wrapper&)' = decltype( std::declval= 0), ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ typename add_const<_Functor>::type&>::type>()( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~ /usr/local/include/c++/6.3.0/functional:600:2: note: candidate: template decltype (std::__invoke(((const std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>)this)->std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::_M_pmf, (forward<_Args>)(std::_Mem_fn_base::operator()::__args)...)) std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _MemFunPtr = A& (std::reference_wrapper::)() const noexcept; bool __is_mem_fn = true] operator()(_Args&&... __args) const ^~~~~~~~ /usr/local/include/c++/6.3.0/functional:600:2: note: template argument deduction/substitution failed: /usr/local/include/c++/6.3.0/functional: In substitution of 'template decltype (std::__invoke(((const std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>)this)->std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::_M_pmf, (forward<_Args>)(std::_Mem_fn_base::operator()::__args)...)) std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::operator()(_Args&& ...) const [with _Args = {std::reference_wrapper&}]': /usr/local/include/c++/6.3.0/functional:999:53: required from here /usr/local/include/c++/6.3.0/functional:603:27: error: no matching function for call to '__invoke(A& (std::reference_wrapper::* const&)() const noexcept, std::reference_wrapper&)' -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...)) ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/include/c++/6.3.0/functional:245:5: note: candidate: template typename std::result_of<_Callable&&(_Args&& ...)>::type std::__invoke(_Callable&&, _Args&& ...) __invoke(_Callable&& __fn, _Args&&... __args) ^~~~~~~~ /usr/local/include/c++/6.3.0/functional:245:5: note: template argument deduction/substitution failed: /usr/local/include/c++/6.3.0/functional: In substitution of 'template typename std::result_of<_Callable&&(_Args&& ...)>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = A& (std::reference_wrapper::* const&)() const noexcept; _Args = {std::reference_wrapper&}]': /usr/local/include/c++/6.3.0/functional:603:27: required by substitution of 'template decltype (std::__invoke(((const std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>)this)->std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::_M_pmf, (forward<_Args>)(std::_Mem_fn_base::operator()::__args)...)) std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::operator()(_Args&& ...) const [with _Args = {std::reference_wrapper&}]' /usr/local/include/c++/6.3.0/functional:999:53: required from here /usr/local/include/c++/6.3.0/functional:245:5: error: no type named 'type' in 'class std::result_of::* const&(std::reference_wrapper&))() const noexcept>' /usr/local/include/c++/6.3.0/functional:1017:2: note: candidate: template _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = std::_Mem_fn::*)() const noexcept>; _Bound_args = {std::_Placeholder<1>}] operator()(_Args&&... __args) volatile ^~~~~~~~ /usr/local/include/c++/6.3.0/functional:1017:2: note: template argument deduction/substitution failed: /usr/local/include/c++/6.3.0/functional:1013:70: error: no match for call to '(volatile std::_Mem_fn::*)() const noexcept>) (std::reference_wrapper&)' = decltype( std::declval= 0), ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ typename add_volatile<_Functor>::type&>::type>()( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~ /usr/local/include/c++/6.3.0/functional:600:2: note: candidate: template decltype (std::__invoke(((const std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>)this)->std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::_M_pmf, (forward<_Args>)(std::_Mem_fn_base::operator()::__args)...)) std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _MemFunPtr = A& (std::reference_wrapper::)() const noexcept; bool __is_mem_fn = true] operator()(_Args&&... __args) const ^~~~~~~~ /usr/local/include/c++/6.3.0/functional:600:2: note: template argument deduction/substitution failed: /usr/local/include/c++/6.3.0/functional: In substitution of 'template decltype (std::__invoke(((const std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>)this)->std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::_M_pmf, (forward<_Args>)(std::_Mem_fn_base::operator()::__args)...)) std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::operator()(_Args&& ...) const [with _Args = {std::reference_wrapper&}]': /usr/local/include/c++/6.3.0/functional:1013:70: required from here /usr/local/include/c++/6.3.0/functional:603:27: error: no matching function for call to '__invoke(A& (std::reference_wrapper::* const&)() const noexcept, std::reference_wrapper&)' -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...)) ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/include/c++/6.3.0/functional:245:5: note: candidate: template typename std::result_of<_Callable&&(_Args&& ...)>::type std::__invoke(_Callable&&, _Args&& ...) __invoke(_Callable&& __fn, _Args&&... __args) ^~~~~~~~ /usr/local/include/c++/6.3.0/functional:245:5: note: template argument deduction/substitution failed: /usr/local/include/c++/6.3.0/functional: In substitution of 'template typename std::result_of<_Callable&&(_Args&& ...)>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = A& (std::reference_wrapper::* const&)() const noexcept; _Args = {std::reference_wrapper&}]': /usr/local/include/c++/6.3.0/functional:603:27: required by substitution of 'template decltype (std::__invoke(((const std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>)this)->std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::_M_pmf, (forward<_Args>)(std::_Mem_fn_base::operator()::__args)...)) std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::operator()(_Args&& ...) const [with _Args = {std::reference_wrapper&}]' /usr/local/include/c++/6.3.0/functional:1013:70: required from here /usr/local/include/c++/6.3.0/functional:245:5: error: no type named 'type' in 'class std::result_of::* const&(std::reference_wrapper&))() const noexcept>' /usr/local/include/c++/6.3.0/functional:1031:2: note: candidate: template _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = std::_Mem_fn::*)() const noexcept>; _Bound_args = {std::_Placeholder<1>}] operator()(_Args&&... __args) const volatile ^~~~~~~~ /usr/local/include/c++/6.3.0/functional:1031:2: note: template argument deduction/substitution failed: /usr/local/include/c++/6.3.0/functional:1027:64: error: no match for call to '(const volatile std::_Mem_fn::*)() const noexcept>) (std::reference_wrapper&)' = decltype( std::declval= 0), ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ typename add_cv<_Functor>::type&>::type>()( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~ /usr/local/include/c++/6.3.0/functional:600:2: note: candidate: template decltype (std::__invoke(((const std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>)this)->std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::_M_pmf, (forward<_Args>)(std::_Mem_fn_base::operator()::__args)...)) std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _MemFunPtr = A& (std::reference_wrapper::)() const noexcept; bool __is_mem_fn = true] operator()(_Args&&... __args) const ^~~~~~~~ /usr/local/include/c++/6.3.0/functional:600:2: note: template argument deduction/substitution failed: /usr/local/include/c++/6.3.0/functional: In substitution of 'template decltype (std::__invoke(((const std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>)this)->std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::_M_pmf, (forward<_Args>)(std::_Mem_fn_base::operator()::__args)...)) std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::operator()(_Args&& ...) const [with _Args = {std::reference_wrapper&}]': /usr/local/include/c++/6.3.0/functional:1027:64: required from here /usr/local/include/c++/6.3.0/functional:603:27: error: no matching function for call to '__invoke(A& (std::reference_wrapper::* const&)() const noexcept, std::reference_wrapper&)' -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...)) ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/include/c++/6.3.0/functional:245:5: note: candidate: template typename std::result_of<_Callable&&(_Args&& ...)>::type std::__invoke(_Callable&&, _Args&& ...) __invoke(_Callable&& __fn, _Args&&... __args) ^~~~~~~~ /usr/local/include/c++/6.3.0/functional:245:5: note: template argument deduction/substitution failed: /usr/local/include/c++/6.3.0/functional: In substitution of 'template typename std::result_of<_Callable&&(_Args&& ...)>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = A& (std::reference_wrapper::* const&)() const noexcept; _Args = {std::reference_wrapper&}]': /usr/local/include/c++/6.3.0/functional:603:27: required by substitution of 'template decltype (std::__invoke(((const std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>*)this)->std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::_M_pmf, (forward<_Args>)(std::_Mem_fn_base::operator()::__args)...)) std::_Mem_fn_base<_MemFunPtr, __is_mem_fn>::operator()(_Args&& ...) const [with _Args = {std::reference_wrapper&}]' /usr/local/include/c++/6.3.0/functional:1027:64: required from here /usr/local/include/c++/6.3.0/functional:245:5: error: no type named 'type' in 'class std::result_of::* const&(std::reference_wrapper&))() const noexcept>'
Aucun commentaire:
Enregistrer un commentaire