当前位置: 动力学知识库 > 问答 > 编程问答 >

How to use boost::is_same in c++ template along with boost::lambda::bind

问题描述:

I'm trying to connect a generic boost::function<void(void)> to many boost::signals2 of varying signature. I'm able to use boot::lambda::bind to do the binding part by passing the return value as part of the bind parameters, but have a problem when the return type is void

Example:

template<typename S>

class signal

{

Connection VoidConnect(boost::function<void(void)> callback)

{

//if(boost::is_same<void, typename S::result_type>::value) // Doesn't seem to work

if ( boost::is_void<typename S::result_type>::value ) // Doesn't seem to work

{

//GetSignal returns the actual boost signal

// typename 'S' will be a boost::function with the same signature as the signal

return GetSignal().connect( boost::lambda::bind(callback) );

}

else

{

typename S::result_type f;

return GetSignal().connect( (boost::lambda::bind(callback), f ) );

}

}

}

I keep getting compilation errors

error: variable or field ‘f’ declared void

include/boost/function/function_template.hpp:132:42: error: void value not ignored as it ought to be

As can be seen from the error, the if condition with boost::is_void (neither is boost::is_same)doesn't seem to work, Does anybody know why? Is there a better way to do this binding?

Thanks,

Sak

网友答案:

You check this condition at runtime, so the both branches need to be correct in the compile-time (and they are obviously not). Instead, just specialize your template for void:

// WARNING: untested code!

template<typename S> 
class signal 
{ 
 Connection VoidConnect(boost::function<void(void)> callback) 
  { 
    typename S::result_type f; 
    return GetSignal().connect( (boost::lambda::bind(callback), f ) ); 
  } 
};

template<> 
class signal <void>
{ 
 Connection VoidConnect(boost::function<void(void)> callback) 
  { 
    return GetSignal().connect( boost::lambda::bind(callback) );         
  } 
};

If you want to use enable_if on the member function, you should define it to be a template. See the following code:

#include <boost/signals2.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_void.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>

using namespace boost::signals2;
class connect
{ 
public:
    template<typename S> 
    connection VoidConnect(boost::function<void(void)> callback, S &sig,
        typename boost::disable_if<boost::is_void<typename S::result_type> >::type *dummy = 0)
    {
    typename S::result_type res;  
    return sig.connect( (boost::lambda::bind(callback), *res) );  
    }

    template<typename S> 
    connection VoidConnect(boost::function<void(void)> callback, S &sig,
        typename boost::enable_if<boost::is_void<typename S::result_type> >::type *dummy = 0)
    {
    return sig.connect( boost::lambda::bind(callback) );          
    }

}; 


int main()
{
    boost::function<void(void)> f;
    signal<void(int)> sig1;
    connect s1;
    s1.VoidConnect(f, sig1);

    signal<int(int)> sig2;
    connect s2;
    s2.VoidConnect(f, sig2);
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: