title: How to use std::bind function as a signal handler in C++?
body: <p>I am using the following code to add signal handling to my C++ classes:</p>
  
  <pre><code>namespace {
      std::atomic&lt;bool&gt; signal_flag(false);   
  }
  void terminate_or_interrupt_handler(int signal) {
      switch (signal) {
          case SIGTERM:
              WARN("SIGTERM received");
              signal_flag.store(true);
              break;
          case SIGINT:
              WARN("SIGINT received");
              signal_flag.store(true);
              break;
          default:
              throw (std::runtime_error("Unhandled signal received"));
      }
  }
  signal(SIGTERM, &amp;terminate_or_interrupt_handler);
  </code></pre>
  
  <p>This code works, but it requires the signal handler function to be define in the same scope as the signal flag variable. I decided to modify the code and pass the <code>signal_flag</code> by reference to the function and use <code>std::bind</code> to "specialize" the handler to my class.</p>
  
  <pre><code> void terminate_or_interrupt_handler(std::atomic&lt;bool&gt;&amp; signal_flag, int signal) {
      switch (signal) {
          case SIGTERM:
              WARN("SIGTERM received");
              signal_flag.store(true);
              break;
          case SIGINT:
              WARN("SIGINT received");
              signal_flag.store(true);
              break;
          default:
              throw (std::runtime_error("Unhandled signal received"));
      }
  }
  auto my_handler = std::bind(terminate_or_interrupt_handler, std::ref(my_class_signal_flag), std::placeholders::_1);
  signal(SIGTERM, &amp;my_handler);
  </code></pre>
  
  <p>However, I get this compile error:</p>
  
  <pre><code>error: cannot convert ‘std::_Bind&lt;void (*(std::reference_wrapper&lt;std::atomic&lt;bool&gt; &gt;, std::_Placeholder&lt;1&gt;))(std::atomic&lt;bool&gt;&amp;, int)&gt;*’ to ‘__sighandler_t’ {aka ‘void (*)(int)’}
  </code></pre>
  
  <p>Is there a way to use a bound function in conjunction with the <code>signal</code> function in C++?</p>