Is it possible to use signal inside a C++ class?

前端 未结 5 1596
独厮守ぢ
独厮守ぢ 2020-12-02 13:19

I am doing something like this:

#include 

class myClass {
public: 
    void myFunction () 
    {
        signal(SIGIO,myHandler);
    }

            


        
5条回答
  •  渐次进展
    2020-12-02 13:53

    Actually, C++ signal handlers are not permitted to use any facilities not present in both C and C++ (except that in C++11 they may use atomics), and are required to use C linkage. Quoting C++11 draft n3242 section 18.10 "Other runtime support" [support.runtime] (paragraph 8),

    The common subset of the C and C++ languages consists of all declarations, definitions, and expressions that may appear in a well formed C++ program and also in a conforming C program. A POF (“plain old function”) is a function that uses only features from this common subset, and that does not directly or indirectly use any function that is not a POF, except that it may use functions defined in Clause 29 that are not member functions. All signal handlers shall have C linkage. A POF that could be used as a signal handler in a conforming C program does not produce undefined behavior when used as a signal handler in a C++ program. The behavior of any other function used as a signal handler in a C++ program is implementation-defined.

    (Clause 29 being the one on atomics.)

提交回复
热议问题