Moving a unordered_map of unique_ptr into another - c++

I'm trying to move a unordered_map of unique_ptr's into another map, but getting the compile error below.
#include <memory>
#include <unordered_map>
#include <string>
int main()
{
std::unordered_map<int, std::unique_ptr<int>> aMap;
std::unordered_map<int, std::unique_ptr<int>> bMap;
std::unique_ptr<int> ptr(new int);
*ptr = 10;
aMap.insert(std::make_pair(0, std::move(ptr)));
std::move(aMap.begin(), aMap.end(), bMap.end());
return 0;
}
1>------ Build started: Project: Testing, Configuration: Debug Win32 ------
1>Build started 08.07.2012 23:54:16.
1>InitializeBuildStatus:
1> Creating "Debug\Testing.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1> main.cpp
1>d:\progs\visual studio 2010\vc\include\utility(260): error C2166: l-value specifies const object
1> d:\progs\visual studio 2010\vc\include\utility(259) : while compiling class template member function 'std::pair<_Ty1,_Ty2> &std::pair<_Ty1,_Ty2>::operator =(std::pair<_Ty1,_Ty2> &&)'
1> with
1> [
1> _Ty1=const int,
1> _Ty2=std::unique_ptr<int>
1> ]
1> d:\coding\testing\main.cpp(12) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1> with
1> [
1> _Ty1=const int,
1> _Ty2=std::unique_ptr<int>
1> ]
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.64
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I think this is an issue that you can't add elements to a map or set by copying to the end. A more simple example that illustrate this is
std::set<int> aSet;
std::set<int> bSet;
aSet.insert(0);
std::move(aSet.begin(), aSet.end(), bSet.end());
to get around this, you need an insert_iterator, so
std::move(aSet.begin(), aSet.end(), inserter(bSet, bSet.begin()));
or
std::move(aMap.begin(), aMap.end(), inserter(bMap, bMap.begin()));
Internally, the insert iterator will attempt to call 'insert' in the passed in container, so rather than trying to do '*(bMap.end()) = *(aMap.begin())', it will call 'bMap.insert(pos, *(aMap.begin()))', though pos is not extremely important for this situation.

Related

How can I avoid the boost::compute::zip_iterator and boost::iterators::zip_iterator confict when using boost compute and boost::range together?

I'd like to use boost::compute and boost::range together, like below, but if I uncomment the #include <boost/range/combine.hpp> line I get an error saying that boost::compute::zip_iterator and boost::iterators::zip_iterator are ambiguous. Is there a way around this, so that I can use boost::compute and boost::range together in the same cpp file? I am using on Windows vs2015 64bit. Boost 1.67.0.
Update: I've found the same error occurs if I try to include boost/iterator/zip_iterator.hpp instead of boost/range/combine.hpp, so it is not specific to the Boost Range library.
Code:
#include <vector>
#include <iostream>
#include <algorithm>
//#include <boost/range/combine.hpp>
#include <boost/compute/lambda.hpp>
#include <boost/compute/functional/math.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/algorithm/transform.hpp>
namespace compute = boost::compute;
using compute::float4_;
using compute::lambda::_1;
using compute::lambda::_2;
using compute::lambda::distance;
int main()
{
// get default device and setup context
compute::device device = compute::system::default_device();
compute::context context(device);
compute::command_queue queue(context, device);
// generate random data on the host
std::vector<float4_> a(10000);
std::vector<float4_> b(10000);
std::vector<float> r(10000);
std::generate((float*)a.data(), (float*)(a.data() + a.size()), rand);
std::generate((float*)b.data(), (float*)(b.data() + b.size()), rand);
// create a vector on the device
compute::vector<float4_> _a(a.size(), context);
compute::vector<float4_> _b(b.size(), context);
compute::vector<float> _r(r.size(), context);
// transfer data from the host to the device
compute::copy(a.begin(), a.end(), _a.begin(), queue);
compute::copy(b.begin(), b.end(), _b.begin(), queue);
boost::compute::transform(
_a.begin(), _a.end(),
_b.begin(),
_r.begin(),
distance(_1, _2),
queue
);
// copy values back to the host
compute::copy(_r.begin(), _r.end(), r.begin(), queue);
for (int i = 0; i < a.size(); ++i)
{
float4_ va = a[i];
float4_ vb = b[i];
float vr = r[i];
float e = std::sqrt(std::pow(va[0] - vb[0], 2) +
std::pow(va[1] - vb[1], 2) +
std::pow(va[2] - vb[2], 2) +
std::pow(va[3] - vb[3], 2));
std::cout << std::setprecision(12);
if (std::abs(e - vr) > 1e-2)
std::cout << e << " != " << vr << "\n";
}
return 0;
}
Error:
1>------ Build started: Project: demo, Configuration: Debug x64 ------
1> demo.cpp 1> This header is implementation detail and provided for
backwards compatibility.
1>C:\local\boost_1_67_0\boost/compute/algorithm/transform.hpp(67):
error C2668: 'boost::compute::make_zip_iterator': ambiguous call to
overloaded function 1>
C:\local\boost_1_67_0\boost/compute/iterator/zip_iterator.hpp(276):
note: could be
'boost::compute::zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>>
boost::compute::make_zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>>(IteratorTuple)'
1> with 1> [ 1>
T=boost::compute::float4_, 1>
IteratorTuple=boost::tuples::tuple,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>
1> ] 1>
C:\local\boost_1_67_0\boost/iterator/zip_iterator.hpp(357): note: or
'boost::iterators::zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>>
boost::iterators::make_zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>>(IteratorTuple)'
[found using argument-dependent lookup] 1> with 1> [
1> T=boost::compute::float4_, 1>
IteratorTuple=boost::tuples::tuple,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>
1> ] 1>
C:\local\boost_1_67_0\boost/compute/algorithm/transform.hpp(67): note:
while trying to match the argument list
'(boost::tuples::tuple,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>)'
1> with 1> [ 1>
T=boost::compute::float4_ 1> ] 1>
C:\workspaces\compute_test\src\demo.cpp(45): note: see reference to
function template instantiation 'OutputIterator
boost::compute::transform,boost::compute::buffer_iterator,boost::compute::buffer_iterator,boost::compute::lambda::expression>(InputIterator1,InputIterator1,InputIterator2,OutputIterator,BinaryOperator,boost::compute::command_queue
&)' being compiled 1> with 1> [ 1>
OutputIterator=boost::compute::buffer_iterator, 1>
T=boost::compute::float4_, 1>
Expr=boost::proto::exprns_::basic_expr,0>>,const
boost::compute::lambda::expression>,0>>
&,const
boost::compute::lambda::expression>,0>>
&>,3>, 1>
InputIterator1=boost::compute::buffer_iterator,
1>
InputIterator2=boost::compute::buffer_iterator,
1>
BinaryOperator=boost::compute::lambda::expression,0>>,const
boost::compute::lambda::expression>,0>>
&,const
boost::compute::lambda::expression>,0>>
&>,3>> 1> ]
1>C:\local\boost_1_67_0\boost/compute/algorithm/transform.hpp(68):
error C2668: 'boost::compute::make_zip_iterator': ambiguous call to
overloaded function 1>
C:\local\boost_1_67_0\boost/compute/iterator/zip_iterator.hpp(276):
note: could be
'boost::compute::zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>>
boost::compute::make_zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>>(IteratorTuple)'
1> with 1> [ 1>
T=boost::compute::float4_, 1>
IteratorTuple=boost::tuples::tuple,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>
1> ] 1>
C:\local\boost_1_67_0\boost/iterator/zip_iterator.hpp(357): note: or
'boost::iterators::zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>>
boost::iterators::make_zip_iterator,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>>(IteratorTuple)'
[found using argument-dependent lookup] 1> with 1> [
1> T=boost::compute::float4_, 1>
IteratorTuple=boost::tuples::tuple,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>
1> ] 1>
C:\local\boost_1_67_0\boost/compute/algorithm/transform.hpp(68): note:
while trying to match the argument list
'(boost::tuples::tuple,boost::compute::buffer_iterator,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>)'
1> with 1> [ 1>
T=boost::compute::float4_ 1> ]
1>C:\local\boost_1_67_0\boost/compute/algorithm/transform.hpp(72):
error C2672: 'transform': no matching overloaded function found
1>C:\local\boost_1_67_0\boost/compute/algorithm/transform.hpp(72):
error C2780: 'OutputIterator
boost::compute::transform(InputIterator1,InputIterator1,InputIterator2,OutputIterator,BinaryOperator,boost::compute::command_queue
&)': expects 6 arguments - 4 provided 1>
C:\local\boost_1_67_0\boost/compute/algorithm/transform.hpp(55): note:
see declaration of 'boost::compute::transform'
========== Build: 0 succeeded,
1 failed, 2 up-to-date, 0 skipped ==========
Short term fix is to modify boost/compute/algorithm/transform.hpp. Change both of the calls to make_zip_iterator to ::boost::compute::make_zip_iterator. This qualifies the call to avoid argument dependent look up.
Update: This is fixed in #790

SFML Thread Initialization not Working

I am attempting to create a thread, but when I compile, the following error appears:
1>------ Build started: Project: GameServer, Configuration: Release Win32 ------
1> Heatbeat.cpp
1>C:\Users\Will\Documents\OpenGL\include\SFML/System/Thread.inl(48): error C2064: term does not evaluate to a function taking 1 arguments
1> C:\Users\Will\Documents\OpenGL\include\SFML/System/Thread.inl(48) : while compiling class template member function 'void sf::priv::ThreadFunctorWithArg<F,A>::run(void)'
1> with
1> [
1> F=void (__thiscall Heartbeat::* )(sf::IpAddress),
1> A=sf::IpAddress
1> ]
1> C:\Users\Will\Documents\OpenGL\include\SFML/System/Thread.inl(79) : see reference to class template instantiation 'sf::priv::ThreadFunctorWithArg<F,A>' being compiled
1> with
1> [
1> F=void (__thiscall Heartbeat::* )(sf::IpAddress),
1> A=sf::IpAddress
1> ]
1> Heatbeat.cpp(26) : see reference to function template instantiation 'sf::Thread::Thread<void(__thiscall Heartbeat::* )(sf::IpAddress),sf::IpAddress>(F,A)' being compiled
1> with
1> [
1> F=void (__thiscall Heartbeat::* )(sf::IpAddress),
1> A=sf::IpAddress
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I am attempting to have the thread accept a function with one argument, but this error is generated. Here is my file:
void Heartbeat::prepareHeartbeat(ClientHandler clients)
{
std::vector<sf::IpAddress> ips;
for(int i = 0; i < clients.size(); i++)
{
PlayerSession player = clients.getPlayers().at(i);
sf::IpAddress ip = player.getIp();
ips.push_back(player.getIp());
std::cout << player.getIp() << std::endl;
sf::Thread thread(&Heartbeat::heartbeat, ip);
thread.launch();
}
}
Any suggestions?
UPDATE: I have tried
sf::Thread thread(&Heartbeat::heartbeat, this, ip);
As well, but this returns the following error:
sf::Thread thread(&Heartbeat::heartbeat, this, ip);
There is a very good SFML threading tutorial here.
Please take a close look at the section "Common mistakes".
Simply put, the syntax does not allow you to do what you want to do with SFML's threads. No constructor takes three arguments.
You will need to std::bind your function and parameters or create a functor or make your heartbeat method static.

C++ Standard library stack usage. Trouble pushing float array

I am having trouble pushing an array to the stack. I thought this was pretty straightforward, but I've already spent too much time trying to figure this one out.
I expected to be able to push arrays just as I push ints or floats, but that is not happening.
The push command is giving me the issue. Here is my code:
#include <iostream>
#include <stack>
struct Matrix4x4
{
float data[16];
};
int main(int argc, char **argv)
{
// My original code
typedef std::stack<float[16]> myStack;
myStack modelViewStack;
myStack projectionStack;
float testMat[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
modelViewStack.push(testMat); // THIS LINE GIVES ME ERRORS
////Stack initialization - This is thokra's solution
////typedef std::stack<std::vector<float[16]>> myStack;
//typedef std::stack<Matrix4x4> myStack;
//myStack modelViewStack;
//myStack projectionStack;
//
//Matrix4x4 m = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
////std::vector<float> testMat2(testMat, testMat + sizeof(testMat) / sizeof(float));
//modelViewStack.push(m);
//for(int i = 0; i<16 ; i++)
//{
// std::cout << "m data: " << m.data[i] << std::endl;
//}
//system("pause");
return 0;
}
Thanks for your help!
Here are the errors. I can't decipher them. Maybe an explanation of how to read these would be helpful too.
1>------ Build started: Project: opengl4_4, Configuration: Release Win32 ------
1> main.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(606): error C2075: 'Target of operator new()' : array initialization needs curly braces
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(605) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty (*),const _Ty (&))'
1> with
1> [
1> _Ty=float [16]
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty (*),const _Ty (&))' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\type_traits(743) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\deque(925) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
1> with
1> [
1> _Ty=std::allocator<float [16]>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\stack(21) : see reference to class template instantiation 'std::deque<_Ty>' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1> main.cpp(14) : see reference to class template instantiation 'std::stack<_Ty>' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(606): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Just encapsulate the data store holding the elements of the matrix in a suitable type:
#include <stack>
struct Matrix4x4
{
float data[16];
};
int main()
{
typedef std::stack<Matrix4x4> myStack;
myStack modelViewStack;
Matrix4x4 m = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
modelViewStack.push(m);
return 0;
}
More to the point: std::stack::push will internally call push_back on the std::deque that's used as the container when you don't change the default. Essentially, when trying to construct a new element at the end of the deque the container tries to place the new element at the address which currently marks the end of the container with a placement-new. For instance, g++ implements it as follows:
template<typename _Up, typename... _Args>
void
construct(_Up* __p, _Args&&... __args)
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
This effectively boils down to:
::new((void *)__p) float[16](_args); // where __p is a pointer to float[16] and _args is testMat
Trying to copy- or move-initialize a C-array is simply not legal. Even if construction succeeded for some reason, the container would try to call the destructor on an element of type float[16] when going out of scope. One can easily see, a destructor ~T[n] does not exist.
In C++11, you can push a std::array<float,16> instead of defining an additional type.

returning a boost ublas matrix by value working only in release config for vs2012

I'm using visual studio 2012 and I isolated a problem in my code to this, but I can't solve it. When running it in release mode it works perfect, but I get an error if I run it in debug.
The code is:
#include "stdafx.h"
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
typedef boost::numeric::ublas::matrix<double> BMT;
BMT fun()
{
BMT mym;
mym.resize(3,3);
for(int i = 0; i<9;++i) mym(i/3,i%3)=i;
std::cout << mym << std::endl;
return mym;
}
int main(int argc, char* argv[])
{
fun();
//closing message
std::cout<<std::endl<<"press enter to exit."<<std::endl;
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
and the error in debug is the following:
1>------ Build started: Project: myproject, Configuration: Debug x64 ------
1> myapp.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory(348): error C4996: 'std::_Uninitialized_copy0': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory(333) : see declaration of 'std::_Uninitialized_copy0'
1> C:\thirdparty\vs2012\x64\boost_1_53_0\boost/numeric/ublas/storage.hpp(94) : see reference to function template instantiation '_FwdIt std::uninitialized_copy<const double*,double*>(_InIt,_InIt,_FwdIt)' being compiled
1> with
1> [
1> _FwdIt=double *,
1> _InIt=const double *
1> ]
1> C:\thirdparty\vs2012\x64\boost_1_53_0\boost/numeric/ublas/storage.hpp(89) : while compiling class template member function 'boost::numeric::ublas::unbounded_array<T>::unbounded_array(const boost::numeric::ublas::unbounded_array<T> &)'
1> with
1> [
1> T=double
1> ]
1> C:\thirdparty\vs2012\x64\boost_1_53_0\boost/numeric/ublas/matrix.hpp(160) : see reference to function template instantiation 'boost::numeric::ublas::unbounded_array<T>::unbounded_array(const boost::numeric::ublas::unbounded_array<T> &)' being compiled
1> with
1> [
1> T=double
1> ]
1> C:\thirdparty\vs2012\x64\boost_1_53_0\boost/numeric/ublas/matrix.hpp(100) : see reference to class template instantiation 'boost::numeric::ublas::unbounded_array<T>' being compiled
1> with
1> [
1> T=double
1> ]
1> junkApp1.cpp(10) : see reference to class template instantiation 'boost::numeric::ublas::matrix<T>' being compiled
1> with
1> [
1> T=double
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Do you know what can the problem be?
I was having a similar issue using ublas.
I am not sure what the cause might be but I suppose it's got to do with ublas' copy on write / copy on demand optimizations. Would it be an option to just use the define -D_SCL_SECURE_NO_WARNINGS and be done with it? I have that set globally as I consider 90% of those warnings OS specifc BS anyway.
The problem is that you are compiling with warnings treated as errors.
EDIT: Microsoft has decided that certain parts of C++ (and C) are deprecated, and the compiler reports use of those parts as errors unless _SCL_SECURE_NO_WARNINGS (respectively _CRT_SECURE_NO_WARNINGS) is defined.

boost::bind, boost::asio, boost::thread, and classes

sau_timer::sau_timer(int secs, timerparam f) : strnd(io),
t(io, boost::posix_time::seconds(secs))
{
assert(secs > 0);
this->f = f;
//t.async_wait(boost::bind(&sau_timer::exec, this, _1));
t.async_wait(strnd.wrap(boost::bind(&sau_timer::exec, this)));
boost::thread thrd(&io,this);
io.run();
//thrd(&sau_timer::start_timer);
}
This is the code I have in the constructor for the class 'sau_timer' (which will hopefully run a timer in a seperate thread and then call another function).
Unfortunately, atm when I try to compile, I get the following error:
1>c:\program files\boost\boost_1_39\boost\bind\bind.hpp(246) : error C2064: term does not evaluate to a function taking 1 arguments
Aswell as a whole bunch of warnings. What am I doing wrong? I've tried everything I can think of, thank you.
The explanation is at the end of the error messages:
c:\users\ben\documents\visual studio 2008\projects\sauria\sauria\sau_timer.cpp(11) :
see reference to function template instantiation
'boost::thread::thread<boost::asio::io_service*,sau_timer*>(F,A1)' being compiled
The error occurs while generating the ctor of boost::thread. It expects a function object (something with an opererator()()), and you pass it what (I guess) is an io::service. If what you want is a thread calling io_service::run, write:
boost::thread thrd(boost::bind(&io_service::run, &io));
If you use a relatively recent version of Boost, I believe that thread's ctor has a convenience overload that takes care of the bind(), allowing to simply write:
boost::thread thrd(&io_service::run, &io);
Each non-static member function has a first, hidden parameter - the instance on which function is to be called. So your exec function needs two arguments. And you have appropriate code, but it is commented out. I mean:
t.async_wait(boost::bind(&sau_timer::exec, this, _1));
Did you try it and had some other problems?
I need it to be used with strnd.wrap() aswell. I've changed it to this again:
sau_timer::sau_timer(int secs, timerparam f) : strnd(io),
t(io, boost::posix_time::seconds(secs))
{
assert(secs > 0);
this->f = f;
t.async_wait(strnd.wrap(boost::bind(&sau_timer::exec, this, _1)));
boost::thread thrd(&io);
io.run();
}
void sau_timer::exec(const boost::system::error_code&) { (f)(params); }
But now I get these errors:
1>------ Build started: Project: Sauria, Configuration: Debug Win32 ------
1>Compiling...
1>sau_timer.cpp
1>Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example:
1>- add -D_WIN32_WINNT=0x0501 to the compiler command line; or
1>- add _WIN32_WINNT=0x0501 to your project's Preprocessor Definitions.
1>Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
1>c:\program files\boost\boost_1_39\boost\bind\bind.hpp(246) : error C2064: term does not evaluate to a function taking 1 arguments
1> c:\program files\boost\boost_1_39\boost\bind\bind_template.hpp(20) : see reference to function template instantiation 'void boost::_bi::list1<A1>::operator ()<F,boost::_bi::list0>(boost::_bi::type<T>,F &,A &,int)' being compiled
1> with
1> [
1> A1=boost::_bi::value<sau_timer *>,
1> F=boost::asio::io_service *,
1> T=void,
1> A=boost::_bi::list0
1> ]
1> c:\program files\boost\boost_1_39\boost\bind\bind_template.hpp(18) : while compiling class template member function 'void boost::_bi::bind_t<R,F,L>::operator ()(void)'
1> with
1> [
1> R=void,
1> F=boost::asio::io_service *,
1> L=boost::_bi::list1<boost::_bi::value<sau_timer *>>
1> ]
1> c:\program files\boost\boost_1_39\boost\thread\detail\thread.hpp(227) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
1> with
1> [
1> R=void,
1> F=boost::asio::io_service *,
1> L=boost::_bi::list1<boost::_bi::value<sau_timer *>>
1> ]
1> c:\users\ben\documents\visual studio 2008\projects\sauria\sauria\sau_timer.cpp(11) : see reference to function template instantiation 'boost::thread::thread<boost::asio::io_service*,sau_timer*>(F,A1)' being compiled
1> with
1> [
1> F=boost::asio::io_service *,
1> A1=sau_timer *
1> ]
1>Build log was saved at "file://c:\Users\Ben\Documents\Visual Studio 2008\Projects\Sauria\Sauria\Debug\BuildLog.htm"
1>Sauria - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========