Does `std::packaged_task` need a CopyConstructible constructor argument? - c++

I have this minimal not-working example of code
#include <future>
int main()
{
auto intTask = std::packaged_task<int()>( []()->int{ return 5; } );
std::packaged_task<void()> voidTask{ std::move(intTask) };
}
Why doesn't it compile (on gcc 4.8.1)? I suspect, the reason is, that std::packaged_task stores the lambda internally inside an std::function which needs a CopyConstructible argument. However, std::packaged_task is move-only. Is this a bug? What does the standard say about it? In my opinion std::packaged_task should not need a CopyConstructible argument, but a MoveConstructible argument should be enough.
By the way, when I replace std::packaged_task<int()> by std::packaged_task<void()> everything compiles fine.
GCC 4.8.1 is giving me this error message:
In file included from /usr/include/c++/4.6/future:38:0,
from ../cpp11test/main.cpp:160:
/usr/include/c++/4.6/functional: In static member function 'static void std::_Function_base::_Base_manager<_Functor>::_M_clone(std::_Any_data&, const std::_Any_data&, std::false_type) [with _Functor = std::packaged_task<int()>, std::false_type = std::integral_constant<bool, false>]':
/usr/include/c++/4.6/functional:1652:8: instantiated from 'static bool std::_Function_base::_Base_manager<_Functor>::_M_manager(std::_Any_data&, const std::_Any_data&, std::_Manager_operation) [with _Functor = std::packaged_task<int()>]'
/usr/include/c++/4.6/functional:2149: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::packaged_task<int()>, _Res = void, _ArgTypes = {}, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<void()>::_Useless]'
/usr/include/c++/4.6/bits/shared_ptr_base.h:410:4: instantiated from 'std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {std::packaged_task<int()>}, _Tp = std::__future_base::_Task_state<void()>, _Alloc = std::allocator<std::__future_base::_Task_state<void()> >, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]'
/usr/include/c++/4.6/bits/shared_ptr_base.h:518:8: instantiated from 'std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = std::__future_base::_Task_state<void()>, _Alloc = std::allocator<std::__future_base::_Task_state<void()> >, _Args = {std::packaged_task<int()>}, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]'
/usr/include/c++/4.6/bits/shared_ptr_base.h:987:35: instantiated from 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<std::__future_base::_Task_state<void()> >, _Args = {std::packaged_task<int()>}, _Tp = std::__future_base::_Task_state<void()>, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]'
/usr/include/c++/4.6/bits/shared_ptr.h:317:64: instantiated from 'std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<std::__future_base::_Task_state<void()> >, _Args = {std::packaged_task<int()>}, _Tp = std::__future_base::_Task_state<void()>]'
/usr/include/c++/4.6/bits/shared_ptr.h:535:39: instantiated from 'std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = std::__future_base::_Task_state<void()>, _Alloc = std::allocator<std::__future_base::_Task_state<void()> >, _Args = {std::packaged_task<int()>}]'
/usr/include/c++/4.6/bits/shared_ptr.h:551:42: instantiated from 'std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = std::__future_base::_Task_state<void()>, _Args = {std::packaged_task<int()>}]'
/usr/include/c++/4.6/future:1223:66: instantiated from 'std::packaged_task<_Res(_ArgTypes ...)>::packaged_task(_Fn&&) [with _Fn = std::packaged_task<int()>, _Res = void, _ArgTypes = {}]'
../cpp11test/main.cpp:165:61: instantiated from here
/usr/include/c++/4.6/functional:1616:4: error: use of deleted function 'std::packaged_task<_Res(_ArgTypes ...)>::packaged_task(std::packaged_task<_Res(_ArgTypes ...)>&) [with _Res = int, _ArgTypes = {}, std::packaged_task<_Res(_ArgTypes ...)> = std::packaged_task<int()>]'
/usr/include/c++/4.6/future:1244:7: error: declared here
UPDATE: I have written the following test program. It seems to support the assumption that the reason is missing CopyConstructability. Again, what are the requirements on the type of the object from which an std::packaged_task may be constructed?
#include <future>
struct Functor {
Functor() {}
Functor( const Functor & ) {} // without this line it doesn't compile
Functor( Functor && ) {}
int operator()(){ return 5; }
};
int main() {
auto intTask = std::packaged_task<int()>( Functor{} );
}

Indeed, packaged_task only has a moving constructor (30.6.9/2):
template <class F> explicit packaged_task(F&& f);
However, your problem is the explicit constructor. So write it like this:
std::packaged_task<int()> pt([]() -> int { return 1; });
Complete example:
#include <future>
#include <thread>
int main()
{
std::packaged_task<int()> intTask([]() -> int { return 5; } );
auto f = intTask.get_future();
std::thread(std::move(intTask)).detach();
return f.get();
}

No. You just can't move a packaged_task<int ()> into a packaged_task<void ()>. These types a unrelated and cannot be move-assigned or move-constructed from each other. If you for some reason really want to do that you can "swallow" the result of the int () like this

The standard (as of N3690) doesn't state anything explicitly about the requirements of the type F in
template <class R, class... ArgTypes>
template <class F>
packaged_task<R(ArgTypes...)>::packaged_task(F&& f);
(see 30.6.9.1) However, it states that
Invoking a copy of f shall behave the same as invoking f.
and that this call can throw
any exceptions thrown by the copy or move constructor of f, or std::bad_alloc if memory
for the internal data structures could not be allocated.
This implicitly implies that the type F must be at least MoveConstructible, or CopyConstructible, if an lvalue reference is handed to the function.
Hence, it's not a bug, it's just not specified that precisely. To solve the problem of putting a std::packaged_task<int()> into a std::packaged_task<void()> just wrap the first into a shared_ptr like this:
#include <future>
#include <memory>
int main()
{
auto intTask = std::make_shared<std::packaged_task<int()>>(
[]()->int{ return 5; } );
std::packaged_task<void()> voidTask{ [=]{ (*intTask)(); } };
}

Related

Passing a function and arguments to a thread

Problem Statement - tl;dr
Add numbers to a vector, then output them
Details
My intention was to make a class Foo, that contained a std::vector<int> that I could populate in a thread-safe manner. I created an AddValue method to allow adding values to that vector keeping thread-safety in mind.
std::mutex mt;
class Foo
{
public:
void AddValue(int i)
{
std::lock_guard<std::mutex> lg{ mt };
values.push_back(i);
}
void PrintValues() const
{
for (int i : values)
{
std::cout << i << " ";
}
}
private:
std::vector<int> values;
};
I then made a free function that I could use to create a thread, without any knowledge of the internals of Foo.
void Func(Foo& foo, int t)
{
for (int i = 0; i < t; i++)
{
foo.AddValue(i);
}
}
My intention was to add the following values to the vector (in whatever order they ended up due to the thread timing)
{3, 2, 2, 1, 1, 1, 0, 0, 0, 0}
Here is a quick test to see if that was working.
int main()
{
Foo foo;
std::vector<std::thread> threads;
for (int i = 1; i < 5; ++i)
{
threads.emplace_back(Func, foo, i);
}
std::for_each(begin(threads), end(threads), [](std::thread& t){ t.join(); });
foo.PrintValues();
}
Problem
The above code won't compile
This is the error message
In file included from /usr/include/c++/4.9/mutex:42:0,
from 3:
/usr/include/c++/4.9/functional: In instantiation of 'struct std::_Bind_simple<void (*(Foo, int))(Foo&, int)>':
/usr/include/c++/4.9/thread:140:47: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(Foo&, int); _Args = {Foo&, int&}]'
/usr/include/c++/4.9/ext/new_allocator.h:120:4: required from 'void __gnu_cxx::new_allocator< <template-parameter-1-1> >::construct(_Up*, _Args&& ...) [with _Up = std::thread; _Args = {void (&)(Foo&, int), Foo&, int&}; _Tp = std::thread]'
/usr/include/c++/4.9/bits/alloc_traits.h:253:4: required from 'static std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::thread; _Args = {void (&)(Foo&, int), Foo&, int&}; _Alloc = std::allocator<std::thread>; std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> = void]'
/usr/include/c++/4.9/bits/alloc_traits.h:399:57: required from 'static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::thread; _Args = {void (&)(Foo&, int), Foo&, int&}; _Alloc = std::allocator<std::thread>; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]'
/usr/include/c++/4.9/bits/vector.tcc:97:40: required from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {void (&)(Foo&, int), Foo&, int&}; _Tp = std::thread; _Alloc = std::allocator<std::thread>]'
44:42: required from here
/usr/include/c++/4.9/functional:1665:61: error: no type named 'type' in 'class std::result_of<void (*(Foo, int))(Foo&, int)>'
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.9/functional:1695:9: error: no type named 'type' in 'class std::result_of<void (*(Foo, int))(Foo&, int)>'
_M_invoke(_Index_tuple<_Indices...>)
^
I don't quite understand that error message. Am I not adding the threads to the vector threads correctly?
You have to wrap the foo in a std::ref. Threads don't allow things to be passed by reference without that wrapper.

Why const member caused error?

#include <vector>
class B {
};
class A {
const std::vector<B> m_v;
public:
A(const std::vector<B>& v) : m_v(v) {}
void doSomething() {}
};
void foo(void* bar) {
A* a = (A*)bar;
a->doSomething();
}
int main() {
std::vector<B> v;
std::vector<A > l;
for (auto i = 0; i < 10; i++) {
A a(v);
l.push_back(std::move(a));
foo((void*)&l[i]);
}
return 0;
}
$ g++ -Wall initList.cpp -o initList -lrt -O3 -std=c++0x
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/vector:70:0,
from initList.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/vector.tcc: In member function \u2018void std::vector::_M_insert_aux(std::vector::iterator, _Args&& ...) [with _Args = {A}, _Tp = A, _Alloc = std::allocator, std::vector::iterator = __gnu_cxx::__normal_iterator >, typename std::_Vector_base::_Tp_alloc_type::pointer = A*]`:
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/vector.tcc:102:4: instantiated from \u2018void std::vector::emplace_back(_Args&& ...) [with _Args = {A}, _Tp = A, _Alloc = std::allocator]`
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_vector.h:840:9: instantiated from \u2018void std::vector::push_back(std::vector::value_type&&) [with _Tp = A, _Alloc = std::allocator, std::vector::value_type = A]`
initList.cpp:23:27: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/vector.tcc:319:4: error: use of deleted function '& A::operator=(const A&)`
initList.cpp:6:7: error: '& A::operator=(const A&)` is implicitly deleted because the default definition would be ill-formed:
initList.cpp:6:7: error: passing \u2018const std::vector` as \u2018this` argument of \u2018std::vector& std::vector::operator=(const std::vector&) [with _Tp = B, _Alloc = std::allocator]` discards qualifiers [-fpermissive]
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/vector:61:0,
from initList.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_algobase.h: In static member function \u2018static _BI2 std::__copy_move_backward::__copy_move_b(_BI1, _BI1, _BI2) [with _BI1 = A*, _BI2 = A*]`:
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_algobase.h:581:18: instantiated from \u2018_BI2 std::__copy_move_backward_a(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = A*, _BI2 = A*]`
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_algobase.h:590:34: instantiated from \u2018_BI2 std::__copy_move_backward_a2(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = A*, _BI2 = A*]`
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_algobase.h:661:15: instantiated from \u2018_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1 = A*, _BI2 = A*]`
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/vector.tcc:313:4: instantiated from \u2018void std::vector::_M_insert_aux(std::vector::iterator, _Args&& ...) [with _Args = {A}, _Tp = A, _Alloc = std::allocator, std::vector::iterator = __gnu_cxx::__normal_iterator >, typename std::_Vector_base::_Tp_alloc_type::pointer = A*]`
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/vector.tcc:102:4: instantiated from \u2018void std::vector::emplace_back(_Args&& ...) [with _Args = {A}, _Tp = A, _Alloc = std::allocator]`
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_vector.h:840:9: instantiated from \u2018void std::vector::push_back(std::vector::value_type&&) [with _Tp = A, _Alloc = std::allocator, std::vector::value_type = A]`
initList.cpp:23:27: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_algobase.h:546:6: error: use of deleted function '& A::operator=(const A&)`
If I don't specify const for m_v. It compiles fine.
The error suggests something with assign operator, which I don't see how it is invoked.
Any idea?
Thanks in advance.
It's a bug in older versions of GCC/libstdc++, your code compiles fine with GCC 4.8.1. Live example. The bug is triggered by
l.push_back(std::move(a));
as older versions of GCC tried to create a default-constructed element in the vector and assigned the parameter std::move(a) to it in a second step instead of using the in-place copy/move-ctor.

is there a reason why std::make_shared would require a default constructor?

I'm trying to figure out if this is a requirement from cereal or not.
I keep getting errors that class Constructors (default ones) are private, which I've put there for a reason.
However, the originating line for the error, seems to be, std::make_shared, rather than cereal, which requires a default constructor, but is already a friend class, and should therefore have access to it.
/usr/include/c++/4.7/type_traits: In instantiation of ‘struct std::__is_default_constructible_impl<Concept>’:
/usr/include/c++/4.7/type_traits:116:12: required from ‘struct std::__and_<std::__not_<std::is_void<Concept> >, std::__is_default_constructible_impl<Concept> >’
/usr/include/c++/4.7/type_traits:682:12: required from ‘struct std::__is_default_constructible_atom<Concept>’
/usr/include/c++/4.7/type_traits:703:12: required from ‘struct std::__is_default_constructible_safe<Concept, false>’
/usr/include/c++/4.7/type_traits:709:12: required from ‘struct std::is_default_constructible<Concept>’
/usr/local/include/cereal/types/polymorphic.hpp:157:5: required by substitution of ‘template<class Archive, class T> typename std::enable_if<((! std::is_default_constructible<T>::value) && (! has_load_and_allocate<T, Archive>())), bool>::type cereal::polymorphic_detail::serialize_wrapper(Archive&, std::shared_ptr<_Tp2>&, uint32_t) [with Archive = cereal::XMLInputArchive; T = Concept]’
/usr/local/include/cereal/types/polymorphic.hpp:253:5: [ skipping 16 instantiation contexts ]
/usr/include/c++/4.7/bits/shared_ptr_base.h:525:8: required from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = SemanticGraph<Concept>; _Alloc = std::allocator<SemanticGraph<Concept> >; _Args = {const char (&)[16]}; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’
/usr/include/c++/4.7/bits/shared_ptr_base.h:997:35: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<SemanticGraph<Concept> >; _Args = {const char (&)[16]}; _Tp = SemanticGraph<Concept>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’
/usr/include/c++/4.7/bits/shared_ptr.h:317:64: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<SemanticGraph<Concept> >; _Args = {const char (&)[16]}; _Tp = SemanticGraph<Concept>]’
/usr/include/c++/4.7/bits/shared_ptr.h:599:39: required from ‘std::shared_ptr<_Tp1> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = SemanticGraph<Concept>; _Alloc = std::allocator<SemanticGraph<Concept> >; _Args = {const char (&)[16]}]’
/usr/include/c++/4.7/bits/shared_ptr.h:615:42: required from ‘std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = SemanticGraph<Concept>; _Args = {const char (&)[16]}]’
/home/alex/projects/Icarus/trunk/Api/ConnectionHandler/../../Processes/Controller/../../Datatypes/Domain/../../Handlers/SemanticNodeFactory/SemanticNodeFactory.hpp:34:82: required from here
/home/alex/projects/Icarus/trunk/Api/ConnectionHandler/../../Processes/Controller/../../Datatypes/Domain/../Episode/../State/../ConceptGraph/../Concept/Concept.hpp:27:5: error: ‘Concept::Concept()’ is private
Can someone please explain to me why this is happening, and more importantly, how could it be resolved, aside from making those constructors public?
EDIT:
originating error line is from:
concept_map = std::make_shared<SemanticGraph<Concept>>( "concept_map.xml" );
Where SemanticGraph Ctor is:
SemanticGraph
(
const std::string filename
)
:
_fname ( filename )
{
std::ifstream input( _fname );
if ( input.is_open() )
{
cereal::XMLInputArchive archive( input );
archive( _nodes );
}
}
C++ used to not consider access control when instantiating templates, except to produce an error if needed. The compiler you're using still uses those rules. Because of that, your class is not considered not-default-constructible. Instead, the check itself is impossible.
GCC 4.8 and higher do support this. A simple demonstration program that succeeds with 4.8, and fails with 4.7 is:
#include <type_traits>
class S { S() {} };
int main() {
return std::is_default_constructible<S>::value;
}
In 4.8, this returns 0. In 4.7, this produces a compile-time error.
To resolve this, make sure you don't have a default constructor, not even a private one. You can add a dummy argument to your constructor, and make sure to always pass that dummy argument.

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).

bind() for using member function as STL comparison function

Could someone tell me why the following won't compile?
#include "a.h"
#include <list>
#include <algorithm>
#include <tr1/functional>
using namespace std;
class B {
public:
B() {
list< A* > aList;
A* a = new A();
lower_bound( aList.begin(), aList.end(), a, tr1::bind( &B::aComp, tr1::placeholders::_1, tr1::placeholders::_2 ) );
}
private:
bool aComp( A* a1, A* a2 );
};
Compile output:
In file included from c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/tr1/functional:56,
from ..\bindTest\/b.h:4,
from ..\bindTest\b.cpp:1:
c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/tr1_impl/functional: In member function 'typename std::tr1::result_of<_Functor(typename std::tr1::result_of<std::tr1::_Mu<_Bound_args, std::tr1::is_bind_expression::value, (std::tr1::is_placeholder::value > 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 = A*&, A* const&, int ..._Indexes = 0, 1, _Functor = std::tr1::_Mem_fn<bool (B::*)(A*, A*)>, _Bound_args = std::tr1::_Placeholder<1>, std::tr1::_Placeholder<2>]':
c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/tr1_impl/functional:1191: instantiated from 'typename std::tr1::result_of<_Functor(typename std::tr1::result_of<std::tr1::_Mu<_Bound_args, std::tr1::is_bind_expression::value, (std::tr1::is_placeholder::value > 0)>(_Bound_args, std::tr1::tuple<_UElements ...>)>::type ...)>::type std::tr1::_Bind<_Functor(_Bound_args ...)>::operator()(_Args& ...) [with _Args = A*, A* const, _Functor = std::tr1::_Mem_fn<bool (B::*)(A*, A*)>, _Bound_args = std::tr1::_Placeholder<1>, std::tr1::_Placeholder<2>]'
c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:2495: instantiated from '_FIter std::lower_bound(_FIter, _FIter, const _Tp&, _Compare) [with _FIter = std::_List_iterator<A*>, _Tp = A*, _Compare = std::tr1::_Bind<std::tr1::_Mem_fn<bool (B::*)(A*, A*)>(std::tr1::_Placeholder<1>, std::tr1::_Placeholder<2>)>]'
..\bindTest\/b.h:13: instantiated from here
c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/tr1_impl/functional:1137: error: no match for call to '(std::tr1::_Mem_fn<bool (B::*)(A*, A*)>) (A*&, A* const&)'
c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/tr1_impl/functional:546: note: candidates are: _Res std::tr1::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&, _ArgTypes ...) const [with _Res = bool, _Class = B, _ArgTypes = A*, A*]
c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/tr1_impl/functional:551: note: _Res std::tr1::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class*, _ArgTypes ...) const [with _Res = bool, _Class = B, _ArgTypes = A*, A*]
aComp is a nonstatic member function of B, so you need to bind to the this pointer as well:
tr1::bind(&B::aComp, this, tr1::placeholders::_1, tr1::placeholders::_2)