Re: [sigc] C++0x lambdas



On 30/08/2011 21:16, Jakub Łukomski wrote:
> Hello. Is there a way to use value returning C++0x lambdas with libsigc++?
> 
> Functions not returning values work fine:
> 
> 	sigc::slot <void> slot1 = [] () { };
> 	sigc::slot <void, int> slot2 = [] (int) { };
> 
> no compilation problems. However when a return value is added:
> 
> 	sigc::slot <bool> slot3 = [] () -> bool { return true; };
> 	sigc::slot <bool, int> slot4 = [] (int) -> bool { return true; };
> 
> I end up getting two compilation errors:
> 	/usr/include/sigc++-2.0/sigc++/functors/slot.h:103:36: error: void value not ignored as it ought to be
> 	/usr/include/sigc++-2.0/sigc++/adaptors/adaptor_trait.h:251:21: error: return-statement with a value, in function returning 'void'
> 
> Is that possible to achieve in some other way, or are lambdas with a return value not supported by libsigc++ yet?

Alright, so I ran into this situation myself and spent the morning without
internet connection figuring this out, and....

-----8<------
#include <type_traits>
#include <sigc++/sigc++.h>

namespace sigc
{
    template <typename Functor>
    struct functor_trait<Functor, false>
    {
        typedef decltype (::sigc::mem_fun (std::declval<Functor&> (),
                                           &Functor::operator())) _intermediate;

        typedef typename _intermediate::result_type result_type;
        typedef Functor functor_type;
    };
}
----->8------

Just put that in a header somewhere and #include it when you want to use
lambdas. It also allows you to throw std::function, boost::function, or any
other object with an appropriate operator() at sigc::signals as well, since
sigc::slot happily wraps them up now.

It still doesn't work with classes that have an overloaded operator(), but you
can always use a lambda for those cases.

-- 
Kind regards,
Loong Jin

Attachment: signature.asc
Description: OpenPGP digital signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]