Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:22:52+00:00 2026-06-04T16:22:52+00:00

I am trying to use the function signal(int,void(*)(int)) from <csignal> to handle the floating

  • 0

I am trying to use the function signal(int,void(*)(int)) from <csignal> to handle the floating point exception SIGFPE. I’d like to be able to print some useful diagnostics besides just a message saying “Floating point exception” or something to that effect. This means the function I pass as the handler to signal needs access to some of the data in my code. Therein lies the rub.

The function must return void and accept only 1 parameter of type int. I cannot make the handler a member function of my data storage class since then the type would be void(Foo::*)(int) due to the hidden this pointer.

I thought about using lambdas to try and make an anonymous function like this;

void handler(int nSig, Foo data)
{
    // do something
}
// snip
Foo data;
signal(SIGFPE, [&](int nSig)->void{handler(nSig,data);});

however because the lambda captures the variable data from outside the compiler will not let it be cast to a pointer to void(*)(int) (which is a shame as this seems like an ideal use for lambdas).

I could simply make data a global variable which could then be seen in handler but I am loath to do this for obvious reasons.

So my question is thus; what is the best way of mimicking anonymous functions in C++?

Note: I would prefer a native C++ solution and not to have to use boost or equivalent.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:22:54+00:00Added an answer on June 4, 2026 at 4:22 pm

    There is no such thing as an anonymous function in C (C++ is irrelevant here, as the function must abide by the C calling convention).

    The only thing you can do is shiver access globals from the handler, probably global variables (and not constants which would be fine).

    I advise making those globals thread local to avoid multithreading issues, but it is still bad in the sense that global variables make for more brittle applications.


    How to ?

    Note: as Luc Danton patiently explained to me, a signal may interrupt any non-atomic activity, and thus reading from a global is safe only if it is a lock-free atomic (or a few other things). Unfortunately std::function may not be so, depending on your implementation, I will still leave this code to explain how it could be done providing that std::function accesses are atomic.

    It is possible to create a trampoline that will call stateful stuff, isolating thread and allowing re-entrant calls.

    typedef std::function<void(int)> SignalHandlerType;
    
    extern thread_local ignalHandlerType SignalHandler;
    

    And we create the following accessor (passed to signal):

    void handle_signal(int const i) {
        if (SignalHandler) { SignalHandler(i); }
    }
    

    as well as the following RAII setter:

    class SignalSetter: boost::noncopyable {
    public:
        SignalSetter(int signal, SignalHandlerType&& sh):
            signal(signal), chandler(0), handler(sh)
        {
            chandler = std::signal(signal, &handle_signal<T>);
            swap(SignalHandler, handler);
        }
    
        ~SignalSetter() {
            std::signal(signal, chandler);
            swap(SignalHandler, handler);
        }
    
    private:
        typedef void(*CHandlerType)(int);
    
        int signal;
        CHandlerType chandler;
        SignalHandlerType handler;
    };
    

    Note: both the global variable and the handle_signal could be private to the SignalSetter class… but since std::signal is not…

    Expected usage:

    int main(int argc, char* argv[]) {
        SignalSetter setter(SIGFPE, [argc, argv]() {
            std::cout << argc << ": " << argc << std::endl;
        });
    
        // do what you want.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use this function from a COM API which enables the
I'm trying to use the header() function to create a redirect. I would like
I am trying to use my own function to get the file size from
All - I am trying to use SciPy's signal.lfilter function to filter a vector
I'm trying to use the function XLookupString . According to the documentation , it
I was trying to use the function glMultiDrawElements while studying OpenGL (using red book)
I am trying to use a function pointer and I am getting this error:
I am trying to use the function Z3_benchmark_to_smtlib_string(). Here are the arguments I am
I'm trying to use the replace function in JavaScript and have a question. strNewDdlVolCannRegion
I am trying to use same validation function for all my controls. But I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.