Passing an object by reference in C++ - c++

Before people start telling me to do a google search for my problem, let me say I've been trying for quite a while.
I can't figure out how to pass an object by reference in C++, I keep getting a massive printout of compiler errors. I can include them if they would help. Specifically, I'm trying to pass the RSSFeed object feed by reference to the function parseFeed in a thread.
void parseFeed(RSSFeed& feed) {
//dostuff
}
RSSFeed feed();
thread(parseFeed, feed); // line 119
errors:
g++ -g -Wall -pedantic -O0 -std=c++0x -D_GLIBCXX_USE_NANOSLEEP -D_GLIBCXX_USE_SCHED_YIELD -I/usr/class/cs110/include/libxml2 -I/usr/class/cs110/local/include -c -o news-aggregator.o news-aggregator.cc
In file included from /usr/include/c++/4.6/functional:56:0,
from /usr/include/c++/4.6/bits/stl_algo.h:68,
from /usr/include/c++/4.6/algorithm:63,
from news-aggregator.cc:14:
rss-feed.h: In constructor âconstexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const _Head&) [with long unsigned int _Idx = 0ul, _Head = RSSFeed]â:
/usr/include/c++/4.6/tuple:162:44: instantiated from âconstexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long unsigned int _Idx = 0ul, _Head = RSSFeed, _Tail = {}]â
/usr/include/c++/4.6/tuple:423:24: instantiated from âconstexpr std::tuple<_T1>::tuple(const _T1&) [with _T1 = RSSFeed]â
/usr/include/c++/4.6/functional:1362:70: instantiated from âstd::_Bind_result<_Result, _Functor(_Bound_args ...)>::_Bind_result(_Functor&&, _Args&& ...) [with _Args = {RSSFeed&}, _Result = void, _Functor = void (*)(RSSFeed&), _Bound_args = {RSSFeed}]â
/usr/include/c++/4.6/functional:1477:41: instantiated from âtypename std::_Bindres_helper<_Result, _Functor, _ArgTypes>::type std::bind(_Functor&&, _ArgTypes&& ...) [with _Result = void, _Functor = void (&)(RSSFeed&), _ArgTypes = {RSSFeed&}, typename std::_Bindres_helper<_Result, _Functor, _ArgTypes>::type = std::_Bind_result<void, void (*(RSSFeed))(RSSFeed&)>]â
/usr/include/c++/4.6/thread:135:9: instantiated from âstd::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(RSSFeed&), _Args = {RSSFeed&}]â
news-aggregator.cc:119:25: instantiated from here
rss-feed.h:54:3: error: âRSSFeed::RSSFeed(const RSSFeed&)â is private
/usr/include/c++/4.6/tuple:97:25: error: within this context
/usr/include/c++/4.6/tuple:97:25: error: use of deleted function âRSSFeed::RSSFeed(const RSSFeed&)â
rss-feed.h:54:3: error: declared here
rss-feed.h: In constructor âstd::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = RSSFeed, long unsigned int _Idx = 0ul, _Head = RSSFeed]â:
/usr/include/c++/4.6/tuple:174:43: instantiated from âstd::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head, _Tail ...>&&) [with long unsigned int _Idx = 0ul, _Head = RSSFeed, _Tail = {}, std::_Tuple_impl<_Idx, _Head, _Tail ...> = std::_Tuple_impl<0ul, RSSFeed>]â
/usr/include/c++/4.6/tuple:434:51: instantiated from âstd::tuple<_T1>::tuple(std::tuple<_T1>&&) [with _T1 = RSSFeed, std::tuple<_T1> = std::tuple<RSSFeed>]â
/usr/include/c++/4.6/functional:1368:78: instantiated from âstd::_Bind_result<_Result, _Functor(_Bound_args ...)>::_Bind_result(std::_Bind_result<_Result, _Functor(_Bound_args ...)>&&) [with _Result = void, _Functor = void (*)(RSSFeed&), _Bound_args = {RSSFeed}, std::_Bind_result<_Result, _Functor(_Bound_args ...)> = std::_Bind_result<void, void (*(RSSFeed))(RSSFeed&)>]â
/usr/include/c++/4.6/functional:1477:41: instantiated from âtypename std::_Bindres_helper<_Result, _Functor, _ArgTypes>::type std::bind(_Functor&&, _ArgTypes&& ...) [with _Result = void, _Functor = void (&)(RSSFeed&), _ArgTypes = {RSSFeed&}, typename std::_Bindres_helper<_Result, _Functor, _ArgTypes>::type = std::_Bind_result<void, void (*(RSSFeed))(RSSFeed&)>]â
/usr/include/c++/4.6/thread:135:9: instantiated from âstd::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(RSSFeed&), _Args = {RSSFeed&}]â
news-aggregator.cc:119:25: instantiated from here
rss-feed.h:54:3: error: âRSSFeed::RSSFeed(const RSSFeed&)â is private
/usr/include/c++/4.6/tuple:101:42: error: within this context
/usr/include/c++/4.6/tuple:101:42: error: use of deleted function âRSSFeed::RSSFeed(const RSSFeed&)â
rss-feed.h:54:3: error: declared here
Resolved:
thread(parseFeed, ref( feed ) ); // line 119

This
RSSFeed feed();
is a function declaration that returns an object of type RSSFeed and has no parameters. It is not an object definition.
Write instead
RSSFeed feed;
Also it seems from the list of error messages that class RSSFeed has no the copy constructor that is it is defined as deleted.

Related

Understanding the compiler errors with std::bind

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);
// ^

GMOCK method is throwing compilation error

When I try to implementation GMOCK method like below I get compilation error but the code and the MOCK class works fine when I remove it:
MOCK_METHOD2(myfunc, void (std::shared_ptr<route>, std::unique_ptr<message, std::default_delete<message> >));
I get the below compilation error:
error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = message; _Dp = std::default_delete<message>]'
MOCK_METHOD2(ship, void (std::shared_ptr<route>, std::unique_ptr<message, std::default_delete<message> >));
^
In file included from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/memory:81:0,
......................................................................................
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/unique_ptr.h:356:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
error: initializing argument 2 of 'R testing::internal::FunctionMocker<R(A1, A2)>::Invoke(A1, A2) [with R = void; A1 = std::shared_ptr<route>; A2 = std::unique_ptr<message>]'
R Invoke(A1 a1, A2 a2) {
^
In file included from ...
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple: In instantiation of 'constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const _Head&) [with long long unsigned int _Idx = 1ull; _Head = std::unique_ptr<message>]':
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple:255:44: recursively required from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long long unsigned int _Idx = 1ull; _Head = std::unique_ptr<message>; _Tail = {}]'
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple:255:44: required from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long long unsigned int _Idx = 0ull; _Head = std::shared_ptr<cmb::mim::MimChannel>; _Tail = {std::unique_ptr<message, std::default_delete<message> >}]'
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple:531:30: required from 'constexpr std::tuple<_T1, _T2>::tuple(const _T1&, const _T2&) [with _T1 = std::shared_ptr<cmb::mim::MimChannel>; _T2 = std::unique_ptr<message>]'
C:/work/googletest/googlemock/include/gmock/gmock-generated-function-mockers.h:122:50: required from 'R testing::internal::FunctionMocker<R(A1, A2)>::Invoke(A1, A2) [with R = void; A1 = std::shared_ptr<cmb::mim::MimChannel>; A2 = std::unique_ptr<message>]'
C:/work/Mock.hpp:39:5: required from here
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple:134:25: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = message; _Dp = std::default_delete<message>]'
: _M_head_impl(__h) { }
I am not sure what the error is and how to fix it - commenting or removing the MOCK method declaration the compilation goes fine?
I was able to resolve the issue by manipulating the function as mentioned below:
MOCK_METHOD2(myfuncproxy, void (std::shared_ptr<route>, message&));
void myfunc((std::shared_ptr<route> r, std::unique_ptr<message, std::default_delete<message> > m)
{
myfuncproxy(r,*m.get());
}

How can I make my own piecewise_construct constructor?

Here's what I have so far for an example implementation of a custom constructor in the style of std::pair::pair(std::piecewise_construct_t, ...):
#include <tuple>
#include <string>
#include <utility>
#include <iostream>
struct A {
std::string m_a;
std::string m_b;
std::string m_c;
A() = default;
A(const A&) = default;
A(A&&) = default;
~A() = default;
template <typename T1, typename T2, typename T3>
A(T1&& a, T2&& b, T3&& c) :
m_a { std::forward<T1>(a) }, m_b { std::forward<T2>(b) }, m_c { std::forward<T3>(c) }
{ }
template <typename... T1, typename... T2, typename... T3,
std::size_t... I1, std::size_t... I2, std::size_t... I3>
A(std::tuple<T1...> args1,
std::tuple<T2...> args2,
std::tuple<T3...> args3,
std::integer_sequence<std::size_t, I1...>,
std::integer_sequence<std::size_t, I2...>,
std::integer_sequence<std::size_t, I3...>) :
m_a { std::forward<T1>(std::get<I1>(args1))... },
m_b { std::forward<T2>(std::get<I2>(args2))... },
m_c { std::forward<T3>(std::get<I3>(args3))... }
{ }
template <typename... T1, typename... T2, typename... T3>
A(std::piecewise_construct_t,
std::tuple<T1...> args1,
std::tuple<T2...> args2,
std::tuple<T3...> args3) :
A { args1, args2, args3,
std::make_index_sequence<sizeof...(T1)>{},
std::make_index_sequence<sizeof...(T2)>{},
std::make_index_sequence<sizeof...(T3)>{} }
{ }
};
int main() {
A a { std::piecewise_construct,
std::forward_as_tuple("abc"),
std::forward_as_tuple(10, 'd'),
std::forward_as_tuple("ef\0\0gh", 6)
};
std::cout << "m_a: " << a.m_a <<
"\nm_b: " << a.m_b <<
"\nm_c: " << a.m_c << '\n';
return 0;
}
Expected output (if piped through cat -v):
m_a: abc
m_b: dddddddddd
m_c: ef^#^#gh
However, gcc is still giving me errors that I can't quite decipher, which appear to be related to the internal implementation of std::tuple:
/tmp/from_tuple.cpp: In instantiation of ‘A::A(std::piecewise_construct_t, std::tuple<_Elements ...>, std::tuple<_Elements ...>, std::tuple<_Tail ...>) [with T1 = {const char (&)[4]}; T2 = {int&&, char&&}; T3 = {const char (&)[7], int&&}]’:
/tmp/from_tuple.cpp:51:11: required from here
/tmp/from_tuple.cpp:42:53: error: use of deleted function ‘constexpr std::tuple<_T1, _T2>::tuple(const std::tuple<_T1, _T2>&) [with _T1 = int&&; _T2 = char&&]’
std::make_index_sequence<sizeof...(T3)>{} }
^
In file included from /tmp/from_tuple.cpp:1:0:
/usr/include/c++/5/tuple:615:17: note: ‘constexpr std::tuple<_T1, _T2>::tuple(const std::tuple<_T1, _T2>&) [with _T1 = int&&; _T2 = char&&]’ is implicitly deleted because the default definition would be ill-formed:
constexpr tuple(const tuple&) = default;
^
/usr/include/c++/5/tuple:615:17: error: use of deleted function ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with long unsigned int _Idx = 0ul; _Head = int&&; _Tail = {char&&}]’
/usr/include/c++/5/tuple:215:17: note: ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with long unsigned int _Idx = 0ul; _Head = int&&; _Tail = {char&&}]’ is implicitly deleted because the default definition would be ill-formed:
constexpr _Tuple_impl(const _Tuple_impl&) = default;
^
/usr/include/c++/5/tuple:215:17: error: use of deleted function ‘constexpr std::_Tuple_impl<_Idx, _Head>::_Tuple_impl(const std::_Tuple_impl<_Idx, _Head>&) [with long unsigned int _Idx = 1ul; _Head = char&&]’
/usr/include/c++/5/tuple:364:17: note: ‘constexpr std::_Tuple_impl<_Idx, _Head>::_Tuple_impl(const std::_Tuple_impl<_Idx, _Head>&) [with long unsigned int _Idx = 1ul; _Head = char&&]’ is implicitly deleted because the default definition would be ill-formed:
constexpr _Tuple_impl(const _Tuple_impl&) = default;
^
/usr/include/c++/5/tuple:364:17: error: use of deleted function ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const std::_Head_base<_Idx, _Head, false>&) [with long unsigned int _Idx = 1ul; _Head = char&&]’
/usr/include/c++/5/tuple:110:17: note: ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const std::_Head_base<_Idx, _Head, false>&) [with long unsigned int _Idx = 1ul; _Head = char&&]’ is implicitly deleted because the default definition would be ill-formed:
constexpr _Head_base(const _Head_base&) = default;
^
/usr/include/c++/5/tuple:110:17: error: copying non-static data member ‘char&& std::_Head_base<1ul, char&&, false>::_M_head_impl’ of rvalue reference type
/usr/include/c++/5/tuple:215:17: error: use of deleted function ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const std::_Head_base<_Idx, _Head, false>&) [with long unsigned int _Idx = 0ul; _Head = int&&]’
constexpr _Tuple_impl(const _Tuple_impl&) = default;
^
/usr/include/c++/5/tuple:110:17: note: ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const std::_Head_base<_Idx, _Head, false>&) [with long unsigned int _Idx = 0ul; _Head = int&&]’ is implicitly deleted because the default definition would be ill-formed:
constexpr _Head_base(const _Head_base&) = default;
^
/usr/include/c++/5/tuple:110:17: error: copying non-static data member ‘int&& std::_Head_base<0ul, int&&, false>::_M_head_impl’ of rvalue reference type
/tmp/from_tuple.cpp:23:7: note: initializing argument 2 of ‘A::A(std::tuple<_Elements ...>, std::tuple<_Elements ...>, std::tuple<_Tail ...>, std::integer_sequence<long unsigned int, I1 ...>, std::integer_sequence<long unsigned int, I2 ...>, std::integer_sequence<long unsigned int, I3 ...>) [with T1 = {const char (&)[4]}; T2 = {int&&, char&&}; T3 = {const char (&)[7], int&&}; long unsigned int ...I1 = {0ul}; long unsigned int ...I2 = {0ul, 1ul}; long unsigned int ...I3 = {0ul, 1ul}]’
A(std::tuple<T1...> args1,
^
/tmp/from_tuple.cpp:42:53: error: use of deleted function ‘constexpr std::tuple<_T1, _T2>::tuple(const std::tuple<_T1, _T2>&) [with _T1 = const char (&)[7]; _T2 = int&&]’
std::make_index_sequence<sizeof...(T3)>{} }
^
In file included from /tmp/from_tuple.cpp:1:0:
/usr/include/c++/5/tuple:615:17: note: ‘constexpr std::tuple<_T1, _T2>::tuple(const std::tuple<_T1, _T2>&) [with _T1 = const char (&)[7]; _T2 = int&&]’ is implicitly deleted because the default definition would be ill-formed:
constexpr tuple(const tuple&) = default;
^
/usr/include/c++/5/tuple:615:17: error: use of deleted function ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with long unsigned int _Idx = 0ul; _Head = const char (&)[7]; _Tail = {int&&}]’
/usr/include/c++/5/tuple:215:17: note: ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with long unsigned int _Idx = 0ul; _Head = const char (&)[7]; _Tail = {int&&}]’ is implicitly deleted because the default definition would be ill-formed:
constexpr _Tuple_impl(const _Tuple_impl&) = default;
^
/usr/include/c++/5/tuple:215:17: error: use of deleted function ‘constexpr std::_Tuple_impl<_Idx, _Head>::_Tuple_impl(const std::_Tuple_impl<_Idx, _Head>&) [with long unsigned int _Idx = 1ul; _Head = int&&]’
/usr/include/c++/5/tuple:364:17: note: ‘constexpr std::_Tuple_impl<_Idx, _Head>::_Tuple_impl(const std::_Tuple_impl<_Idx, _Head>&) [with long unsigned int _Idx = 1ul; _Head = int&&]’ is implicitly deleted because the default definition would be ill-formed:
constexpr _Tuple_impl(const _Tuple_impl&) = default;
^
/usr/include/c++/5/tuple:364:17: error: use of deleted function ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const std::_Head_base<_Idx, _Head, false>&) [with long unsigned int _Idx = 1ul; _Head = int&&]’
/usr/include/c++/5/tuple:110:17: note: ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const std::_Head_base<_Idx, _Head, false>&) [with long unsigned int _Idx = 1ul; _Head = int&&]’ is implicitly deleted because the default definition would be ill-formed:
constexpr _Head_base(const _Head_base&) = default;
^
/usr/include/c++/5/tuple:110:17: error: copying non-static data member ‘int&& std::_Head_base<1ul, int&&, false>::_M_head_impl’ of rvalue reference type
/tmp/from_tuple.cpp:23:7: note: initializing argument 3 of ‘A::A(std::tuple<_Elements ...>, std::tuple<_Elements ...>, std::tuple<_Tail ...>, std::integer_sequence<long unsigned int, I1 ...>, std::integer_sequence<long unsigned int, I2 ...>, std::integer_sequence<long unsigned int, I3 ...>) [with T1 = {const char (&)[4]}; T2 = {int&&, char&&}; T3 = {const char (&)[7], int&&}; long unsigned int ...I1 = {0ul}; long unsigned int ...I2 = {0ul, 1ul}; long unsigned int ...I3 = {0ul, 1ul}]’
A(std::tuple<T1...> args1,
^
Whoops, it looks like I just needed a bit more patience looking at the error messages. If I'd just looked at the first one, it was telling me that std::tuple<int&&, char&&> has an implicitly deleted copy constructor, presumably because of the rvalue-reference members. So, if I change the overload taking dummy integer_sequence objects to take tuple references, then the example compiles.
But then, in the case of the second element, it looks like it's using the initializer_list constructor instead of the intended size_t, char constructor. Changing from brace-initializers to the old-style parenthesized initializers in the same overload fixed that.
So overall, replace that overload with:
template <typename... T1, typename... T2, typename... T3,
std::size_t... I1, std::size_t... I2, std::size_t... I3>
A(std::tuple<T1...>& args1,
std::tuple<T2...>& args2,
std::tuple<T3...>& args3,
std::integer_sequence<std::size_t, I1...>,
std::integer_sequence<std::size_t, I2...>,
std::integer_sequence<std::size_t, I3...>) :
m_a(std::forward<T1>(std::get<I1>(args1))...),
m_b(std::forward<T2>(std::get<I2>(args2))...),
m_c(std::forward<T3>(std::get<I3>(args3))...)
{ }

How to create std::thread with function pointer with a reference?

class taskq {
public:
int trigger(taskq &tq);
mutex mtx;
};
void func_wrapper(taskq &tq) {
cout<<endl;
}
int taskq::trigger(taskq &tq) {
thread thread(func_wrapper, tq);
return 0;
}
I tried to compile the above simple code but I am keep getting errors because of line thread thread(func_wrapper, tq);.
What's wrong with this code?
How could I fix it?
The error message is as below:
In file included from /usr/include/c++/4.9/thread:39:0,
from taskq.C:2:
/usr/include/c++/4.9/functional: In instantiation of ‘struct std::_Bind_simple<void (*(taskq))(taskq&)>’:
/usr/include/c++/4.9/thread:140:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}]’
taskq.C:20:33: required from here
/usr/include/c++/4.9/functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<void (*(taskq))(taskq&)>’
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 (*(taskq))(taskq&)>’
_M_invoke(_Index_tuple<_Indices...>)
^
In file included from /usr/include/c++/4.9/functional:55:0,
from /usr/include/c++/4.9/thread:39,
from taskq.C:2:
/usr/include/c++/4.9/tuple: In instantiation of ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = taskq; <template-parameter-2-2> = void; long unsigned int _Idx = 1ul; _Head = taskq]’:
/usr/include/c++/4.9/tuple:271:42: required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head, _Tail ...>&&) [with long unsigned int _Idx = 1ul; _Head = taskq; _Tail = {}]’
/usr/include/c++/4.9/type_traits:900:43: required by substitution of ‘template<class _Tp, class _Arg, class> static std::true_type std::__do_is_direct_constructible_impl::__test(int) [with _Tp = std::_Tuple_impl<1ul, taskq>; _Arg = std::_Tuple_impl<1ul, taskq>&&; <template-parameter-1-3> = <missing>]’
/usr/include/c++/4.9/type_traits:912:43: required from ‘struct std::__is_direct_constructible_impl<std::_Tuple_impl<1ul, taskq>, std::_Tuple_impl<1ul, taskq>&&>’
/usr/include/c++/4.9/type_traits:134:12: required from ‘struct std::__and_<std::is_destructible<std::_Tuple_impl<1ul, taskq> >, std::__is_direct_constructible_impl<std::_Tuple_impl<1ul, taskq>, std::_Tuple_impl<1ul, taskq>&&> >’
/usr/include/c++/4.9/type_traits:916:12: required from ‘struct std::__is_direct_constructible_new_safe<std::_Tuple_impl<1ul, taskq>, std::_Tuple_impl<1ul, taskq>&&>’
/usr/include/c++/4.9/type_traits:994:12: [ skipping 7 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/4.9/type_traits:1175:12: required from ‘struct std::is_nothrow_move_constructible<std::_Tuple_impl<1ul, taskq> >’
/usr/include/c++/4.9/type_traits:134:12: required from ‘struct std::__and_<std::is_nothrow_move_constructible<void (*)(taskq&)>, std::is_nothrow_move_constructible<std::_Tuple_impl<1ul, taskq> > >’
/usr/include/c++/4.9/tuple:267:7: required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head, _Tail ...>&&) [with long unsigned int _Idx = 0ul; _Head = void (*)(taskq&); _Tail = {taskq}]’
/usr/include/c++/4.9/functional:1727:41: required from ‘typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type std::__bind_simple(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}; typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type = std::_Bind_simple<void (*(taskq))(taskq&)>]’
/usr/include/c++/4.9/thread:140:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}]’
taskq.C:20:33: required from here
/usr/include/c++/4.9/tuple:140:42: error: use of deleted function ‘taskq::taskq(taskq&&)’
: _M_head_impl(std::forward<_UHead>(__h)) { }
^
taskq.C:9:7: note: ‘taskq::taskq(taskq&&)’ is implicitly deleted because the default definition would be ill-formed:
class taskq {
^
taskq.C:9:7: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
In file included from taskq.C:3:0:
/usr/include/c++/4.9/mutex:129:5: note: declared here
mutex(const mutex&) = delete;
^
In file included from /usr/include/c++/4.9/functional:55:0,
from /usr/include/c++/4.9/thread:39,
from taskq.C:2:
/usr/include/c++/4.9/tuple: In instantiation of ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const _Head&) [with long unsigned int _Idx = 1ul; _Head = taskq]’:
/usr/include/c++/4.9/tuple:255:44: recursively required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long unsigned int _Idx = 1ul; _Head = taskq; _Tail = {}]’
/usr/include/c++/4.9/tuple:255:44: required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long unsigned int _Idx = 0ul; _Head = void (*)(taskq&); _Tail = {taskq}]’
/usr/include/c++/4.9/tuple:531:30: required from ‘constexpr std::tuple<_T1, _T2>::tuple(const _T1&, const _T2&) [with _T1 = void (*)(taskq&); _T2 = taskq]’
/usr/include/c++/4.9/functional:1678:74: required from ‘std::_Bind_simple<_Callable(_Args ...)>::_Bind_simple(_Callable&&, _Args2&& ...) [with _Args2 = {taskq&}; <template-parameter-2-2> = void; _Callable = void (*)(taskq&); _Args = {taskq}]’
/usr/include/c++/4.9/functional:1727:41: required from ‘typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type std::__bind_simple(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}; typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type = std::_Bind_simple<void (*(taskq))(taskq&)>]’
/usr/include/c++/4.9/thread:140:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}]’
taskq.C:20:33: required from here
/usr/include/c++/4.9/tuple:134:25: error: use of deleted function ‘taskq::taskq(const taskq&)’
: _M_head_impl(__h) { }
^
taskq.C:9:7: note: ‘taskq::taskq(const taskq&)’ is implicitly deleted because the default definition would be ill-formed:
class taskq {
^
taskq.C:9:7: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
In file included from taskq.C:3:0:
/usr/include/c++/4.9/mutex:129:5: note: declared here
mutex(const mutex&) = delete;
^
make: *** [taskq.o] Error 1
thread thread(func_wrapper, std::ref(tq));
Note that you need to make sure the reference is still valid while you are executing func_wrapper (since I'm guessing such function uses tq).
std::thread thread([&tq]() {
func_wrapper(tq);
});

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