Sign in to follow this  
Diltsman

using std::bind1st

Recommended Posts

I'm having a bit of a problem with std::bind1st. I'm trying to create a thread using bind1st to bind a stream to the first argument of a 1 argument function, and then use that for executing the child thread. However I get errors. What am I doing wrong? I verified that the function passed to callback meets the requirement of taking a single iostream object as a parameter.
template<typename T>
void Socket::ServerSocket::ProcessRequests(T callback) const
{
	while(listen(this->m_socket, SOMAXCONN) != SOCKET_ERROR)
	{
		Socket::ServerSocket::ServerSocketDevice device(this->m_socket);
		boost::iostreams::stream_buffer<Socket::ServerSocket::ServerSocketDevice> buffer;
		buffer.open(device);
		std::iostream stream(&buffer);
		boost::thread child(std::bind1st(callback, stream));
	}


Error	1	error C2825: '_Fn2': must be a class or namespace when followed by '::'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	261	
Error	2	error C2039: 'second_argument_type' : is not a member of '`global namespace''	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	261	
Error	3	error C2146: syntax error : missing ',' before identifier 'second_argument_type'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	261	
Error	4	error C2065: 'second_argument_type' : undeclared identifier	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	261	
Error	5	error C2825: '_Fn2': must be a class or namespace when followed by '::'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	262	
Error	6	error C2039: 'result_type' : is not a member of '`global namespace''	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	262	
Error	7	error C2146: syntax error : missing ',' before identifier 'result_type'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	262	
Error	8	error C2065: 'result_type' : undeclared identifier	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	262	
Error	9	error C2955: 'std::unary_function' : use of class template requires template argument list	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	263	
Error	10	error C2825: '_Fn2': must be a class or namespace when followed by '::'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	265	
Error	11	error C2143: syntax error : missing ',' before '`global namespace'::second_argument_type'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	265	
Error	12	error C2825: '_Fn2': must be a class or namespace when followed by '::'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	266	
Error	13	error C2143: syntax error : missing ',' before '`global namespace'::result_type'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	266	
Error	14	error C2955: 'std::unary_function' : use of class template requires template argument list	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	267	
Error	15	error C2955: 'std::unary_function' : use of class template requires template argument list	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	268	
Error	16	error C2825: '_Fn2': must be a class or namespace when followed by '::'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	270	
Error	17	error C2825: '_Fn2': must be a class or namespace when followed by '::'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	270	
Error	18	error C2039: 'first_argument_type' : is not a member of '`global namespace''	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	270	
Error	19	error C2143: syntax error : missing ',' before '&'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	270	
Error	20	error C2825: '_Fn2': must be a class or namespace when followed by '::'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	288	
Error	21	error C2039: 'first_argument_type' : is not a member of '`global namespace''	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	288	
Error	22	error C2146: syntax error : missing ';' before identifier 'value'	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	288	
Error	23	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\program files (x86)\microsoft visual studio 8\vc\include\functional	288	
Error	24	error C2664: 'boost::thread::thread(const boost::function0<R> &)' : cannot convert parameter 1 from 'std::binder1st<_Fn2>' to 'const boost::function0<R> &'	c:\users\diltsman\documents\visual studio 2005\projects\operationaardvark\socketstream\serversocket.h	58	



Share this post


Link to post
Share on other sites
Copy and paste the compiler output, as opposed to the error list. The latter prints only the first line of each error, making it close to useless for fixing template errors. Also, what's the code you're using it with?

Share this post


Link to post
Share on other sites
If T is a function pointer type in your particular template instantiation, you'll need to wrap callback with ptr_fun() at some point as bind1st and friends expect functors with appropriate internal typedefs.


// untested
template<typename Funtoid, bool IsFuncPtr>
struct start_thread
{
static void run(Functoid f, std::iostream &stream)
{
boost::thread(std::bind1st(f, stream));
}
};

template<typename Funtoid>
struct start_thread<Functoid, true>
{
static void run(Functoid f, std::iostream &stream)
{
boost::thread(std::bind1st(std::ptr_fun(f), stream));
}
};

template<typename Functoid>
void make_thread(Functoid f, std::iosream &stream)
{
const bool is_ptr = boost::is_pointer<Functoid>::value;
start_thread<Functoid, is_ptr>::run(f, stream);
}

template<typename T>
void Socket::ServerSocket::ProcessRequests(T callback) const
{
while(listen(this->m_socket, SOMAXCONN) != SOCKET_ERROR)
{
Socket::ServerSocket::ServerSocketDevice device(this->m_socket);
boost::iostreams::stream_buffer<Socket::ServerSocket::ServerSocketDevice> buffer;
buffer.open(device);
std::iostream stream(&buffer);
make_thread(callback, stream);
}

// ...





EDIT: or just use boost::bind

Also, I'm not sure the way you have things set up is valid. The stream you pass in may be destroyed during the child threads execution i.e. it may end up operating on a dangling reference.

Share this post


Link to post
Share on other sites
Quote:
Original post by the_edd
If T is a function pointer type in your particular template instantiation, you'll need to wrap callback with ptr_fun() at some point as bind1st and friends expect functors with appropriate internal typedefs.

*** Source Snippet Removed ***

EDIT: or just use boost::bind

Also, I'm not sure the way you have things set up is valid. The stream you pass in may be destroyed during the child threads execution i.e. it may end up operating on a dangling reference.


Thanks for the suggestion. I changed to boost::bind, and that fixed most of it. As for the stream concern, I figured that would be a problem, but couldn't get far enough to tell for sure.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this