[boost::function] problem with void type as argument type
Hi! I have small piece of code and I do not know why it is not compiling - proper instantiation of function is not created. It means, why boost::function can not deduct types (code below). The problem is when type U is void. Can any one help me with it? Regards. template < typename T, typename U > void test ( boost::function<T(U)> & f ) { f(); } int f3() { std::cout << "f3(void)" << std::endl; return 3; } void f4() { std::cout << "f4(void)" << std::endl; return;// 4; } int main() { boost::function<int (void)> h = &f3; boost::function<void (void)> i = &f4; test (h); test (i); } -- |\/\/| Seweryn Habdank-Wojewódzki \/\/
On Mon, 2007-03-26 at 20:31 +0200, Seweryn Habdank-Wojewódzki wrote:
Hi!
I have small piece of code and I do not know why it is not compiling - proper instantiation of function is not created.
It means, why boost::function can not deduct types (code below).
The problem is when type U is void. Can any one help me with it?
Regards.
template < typename T, typename U > void test ( boost::function<T(U)> & f ); [snip] boost::function<int (void)> h = &f3; boost::function<void (void)> i = &f4;
The problem is that "int(void)" is just an alternative spelling for "int()", i.e., a function type for a function that takes no parameters but returns an "int". A function type int() cannot match a template parameter T(U), because there is nothing to match the "U". Cheers, Doug
Hi! Douglas Gregor wrote:
template < typename T, typename U > void test ( boost::function<T(U)> & f ); [snip] boost::function<int (void)> h = &f3; boost::function<void (void)> i = &f4;
The problem is that "int(void)" is just an alternative spelling for "int()", i.e., a function type for a function that takes no parameters but returns an "int". A function type int() cannot match a template parameter T(U), because there is nothing to match the "U".
Thanks for reply. However still I do not know how to proceed in that case, because functions can not be partially specialized. Is there any opportunity to prepare more less such a function which will automatically detects arguments of boost::function template? Best regards. -- |\/\/| Seweryn Habdank-Wojewódzki \/\/
On Mon, 2007-03-26 at 21:39 +0200, Seweryn Habdank-Wojewódzki wrote:
Hi!
Douglas Gregor wrote:
template < typename T, typename U > void test ( boost::function<T(U)> & f ); [snip] boost::function<int (void)> h = &f3; boost::function<void (void)> i = &f4;
The problem is that "int(void)" is just an alternative spelling for "int()", i.e., a function type for a function that takes no parameters but returns an "int". A function type int() cannot match a template parameter T(U), because there is nothing to match the "U".
Thanks for reply. However still I do not know how to proceed in that case, because functions can not be partially specialized. Is there any opportunity to prepare more less such a function which will automatically detects arguments of boost::function template?
You can write multiple overloads of your "test" function, each of which accepts boost::function instances with a different number of function arguments: template<typename R> void test (boost::function<T(void)>& f); template<typename R, typename T1> void test (boost::function<T(T1)>& f); template<typename R, typename T1, typename T2> void test (boost::function<T(T1, T2)>& f); template<typename R, typename T1, typename T2, typename T3> void test (boost::function<T(T1, T2, T3)>& f); Tedious, unfortunately, but it works. Cheers, Doug
Hi! Douglas Gregor wrote:
Thanks for reply. However still I do not know how to proceed in that case, because functions can not be partially specialized. Is there any opportunity to prepare more less such a function which will automatically detects arguments of boost::function template?
You can write multiple overloads of your "test" function, each of which accepts boost::function instances with a different number of function arguments:
template<typename R> void test (boost::function<T(void)>& f);
template<typename R, typename T1> void test (boost::function<T(T1)>& f);
template<typename R, typename T1, typename T2> void test (boost::function<T(T1, T2)>& f);
template<typename R, typename T1, typename T2, typename T3> void test (boost::function<T(T1, T2, T3)>& f);
Tedious, unfortunately, but it works.
Ok. It is long, but good enough. Thanks and kind regards. -- |\/\/| Seweryn Habdank-Wojewódzki \/\/
participants (2)
-
Douglas Gregor
-
Seweryn Habdank-Wojewódzki