feenableexcept etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
feenableexcept etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

26 Ağustos 2019 Pazartesi

GNU feenableexcept

Giriş
Açıklaması şöyle.
GNU libc function feenableexcept() enables trapping of the floating-point exceptions, which generates the signal SIGFPE. If the compiler option -fnon-call-exceptions was used, the handler for that signal may throw a user-defined C++ exception.
Önce şöyle yaparız.
#include <cfenv>

feenableexcept(FE_INVALID);
feenableexcept() ile SIGFPE ürettikten sonra kendi exception'ımızı fırlatmak için şöyle yaparız
#include <signal.h>
#include <memory>
#include <iostream>

int main() {
    std::shared_ptr<void(int)> handler(
        signal(SIGFPE, [](int signum) {throw std::logic_error("FPE"); }),
        [](__sighandler_t f) { signal(SIGFPE, f); });

    int i = 0;

    std::cin >> i;  // what if someone enters zero?

    try {
        i = 5/i;
    }
    catch (std::logic_error e) {
        std::cerr << e.what();
    }
}
Derlemek için şöyle yaparız
g++ -c Foo.cc -o Foo.o -fnon-call-exceptions -std=c++11