0

有以下代码。该Test函数可以接受或不接受插槽作为参数。有没有办法实现相同但只使用boost::signal2库?

#include <boost/signals2.hpp>
#include <boost/optional.hpp>
#include <string>
#include <iostream>

typedef boost::signals2::signal<std::string (void)> CSignal;
typedef CSignal::slot_type CSlotType;
typedef boost::optional<CSlotType> COptSlotType;

void Test(const COptSlotType &optSlot = COptSlotType()) {
    std::string str;
    if (optSlot) {
        CSignal sig;
        sig.connect(*optSlot);
        const auto rt = sig();
        if (rt) {
            str = *rt;
        }
    }
    else {
        str = "No Slot";
    }
    std::cout << str << std::endl;
}

std::string TestSignal(void) {
    return "Signal";
}

int main(int argc, char *argv[]) {
    Test();
    Test(COptSlotType(&TestSignal));
    return 0;
}
4

0 回答 0