根据lambda的结果过载

时间:2015-01-04 08:24:39

标签: c++ c++11 lambda

我有一个类:

class foo
{
    EventManager<LRESULT(UINT, WPARAM, LPARAM)> Events;

    template<typename T>
    void addListener(int e, T&& lambda) { events.add(e, lambda); }
};

但是,我需要接受两种类型的lambdas。一个有签名:

[&](UINT, WPARAM, LPARAM) -> LRESULT {},其中一个签名为:[&](UINT, WPARAM, LPARAM) -> void {}

所以我想确定lambdas的返回类型..

我尝试过类似的事情:

template<typename T>
void addListener(int e, T&& listener)
{
    if (std::is_void<decltype(listener(0, 0, 0))>::value)
    {
        Events.Subscribe(e, [&](UINT msg, WPARAM wp, LPARAM lp) -> LRESULT {
            listener(msg, wp, lp);
            return DefWindowProcW(this->Handle(), msg, wp, lp);
        });
    }

    Events.Subscribe(e, [&](UINT msg, WPARAM wp, LPARAM lp) -> LRESULT {
        std::function<LRESULT()> func = std::bind(listener, msg, wp, lp);
        return func();
    });
}

我还尝试将该函数声明为具有以下签名的模板:

template<typename T, typename = typename std::enable_if<std::is_void<typename std::result_of<T(UINT, WPARAM, LPARAM)>::type>::value>::type>

重载它但它也不起作用..

我可以做什么,而不必创建两个方法(每个签名一个)?

1 个答案:

答案 0 :(得分:5)

选项#1

将呼叫转移到适当的过载:

#include <functional>
#include <type_traits>
#include <utility>

class foo
{
public:
    template <typename T>
    void addListener(int e, T&& listener)
    {
        addListener(e, std::forward<T>(listener), std::is_void<decltype(listener(0, 0, 0))>{});
    }

private:
    EventManager<LRESULT(UINT, WPARAM, LPARAM)> Events;

    template <typename T>
    void addListener(int e, T&& listener, std::true_type)
    {
        Events.Subscribe(e, [=](UINT msg, WPARAM wp, LPARAM lp) -> LRESULT {
            listener(msg, wp, lp);
            return DefWindowProcW(this->Handle(), msg, wp, lp);
        });
    }

    template <typename T>
    void addListener(int e, T&& listener, std::false_type)
    {
        Events.Subscribe(e, [=](UINT msg, WPARAM wp, LPARAM lp) -> LRESULT {
            std::function<LRESULT()> func = std::bind(listener, msg, wp, lp);
            return func();
        });
    }
};

DEMO 1

选项#2

使用SFINAE条件添加重载(如果要在第二个重载中隐藏模板参数列表中的typename = void,则需要伪类型模板参数enable_if,以使函数模板声明不同):

#include <functional>
#include <type_traits>

class foo
{
public:    
    template <typename T,
              typename = typename std::enable_if<std::is_void<typename std::result_of<T(UINT, WPARAM, LPARAM)>::type>::value>::type>
    void addListener(int e, T&& listener)
    {
        Events.Subscribe(e, [=](UINT msg, WPARAM wp, LPARAM lp) -> LRESULT {
            listener(msg, wp, lp);
            return DefWindowProcW(this->Handle(), msg, wp, lp);
        });
    }

    template <typename T,
              typename = typename std::enable_if<!std::is_void<typename std::result_of<T(UINT, WPARAM, LPARAM)>::type>::value>::type,
              typename = void>
    void addListener(int e, T&& listener)
    {
        Events.Subscribe(e, [=](UINT msg, WPARAM wp, LPARAM lp) -> LRESULT {
            std::function<LRESULT()> func = std::bind(listener, msg, wp, lp);
            return func();
        });
    }

private:
    EventManager<LRESULT(UINT, WPARAM, LPARAM)> Events;
};

DEMO 2

选项#3

使用带有decltype()说明符的尾随返回类型来启用/禁用重载:

#include <functional>
#include <type_traits>
#include <utility>

class foo
{
public:    
    template <typename T>
    auto addListener(int e, T&& listener)
        -> typename std::enable_if<std::is_void<decltype(std::forward<T>(listener)(0, 0, 0))>{}>::type
    {
        Events.Subscribe(e, [=](UINT msg, WPARAM wp, LPARAM lp) -> LRESULT {
            listener(msg, wp, lp);
            return DefWindowProcW(this->Handle(), msg, wp, lp);
        });
    }

    template <typename T>
    auto addListener(int e, T&& listener)
        -> typename std::enable_if<!std::is_void<decltype(std::forward<T>(listener)(0, 0, 0))>{}>::type
    {
        Events.Subscribe(e, [=](UINT msg, WPARAM wp, LPARAM lp) -> LRESULT {
            std::function<LRESULT()> func = std::bind(listener, msg, wp, lp);
            return func();
        });
    }

private:
    EventManager<LRESULT(UINT, WPARAM, LPARAM)> Events;
};

DEMO 3