c++11 信号是否等同于增强信号

Are c++11 signals equivalent to boost.signals

本文关键字:信号 增强 等同于 是否 c++11      更新时间:2023-10-16

正如我所理解的那样,c ++ 11标准中包含许多增强库。将boost::更改为std::后,我设法使用 c++11 编译器编译它们。我在编译包含boost::signals的代码时遇到问题。

#include <iostream> 
#include <functional>
#include <csignal>
void func()
{
    std::cout << "Hello world" << std::endl;
}
int main() 
{ 
    std::signal<void()> s;
    s.connect(func);
    s();      
} 

我收到此错误:

prog.cpp: In function ‘int main()’:
prog.cpp:12:19: error: invalid operands of types ‘void (*(int, __sighandler_t)throw ())(int) {aka void (*(int, void (*)(int))throw ())(int)}’ and ‘void’ to binary ‘operator<’
  std::signal<void()> s;
                   ^
prog.cpp:12:22: error: ‘s’ was not declared in this scope
  std::signal<void()> s; 

std::signal不等于boost::signal吗?

std::signal 甚至不是一个模板,你不能写std::signal<type>;

http://en.cppreference.com/w/cpp/utility/program/signal

它们是完全不同的东西。 boost::signal 是一个信号槽框架,而 std::signal(它来自 C,而不是来自 C++11)是一个设置操作系统信号处理程序的函数。

std::signal

boost::signal无关。但是在不使用boost的情况下制作像boost::signal这样的东西并不难

template <typename... T>
struct signal{
    typedef std::function<T...> function_type;
    typedef std::vector<function_type> container_type;
    container_type _slots;
    template <typename... Args>
    void operator()(Args... args){
        for(function_type f: _slots){
            f(args...);
        }
    }
    void connect(function_type slot){
        _slots.push_back(slot);
    }
};