Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
18.limits.traps.stdcxx-624.cpp
Go to the documentation of this file.
1
27#include <cassert> // for assert()
28#include <csignal> // for SIGFPE and signal()
29#include <cstdlib> // for strtod()
30#include <limits> // for numeric_limits
31
32
33char digits[] = " +0.?23456789e+01";
34
35volatile int result;
36
37
38extern "C" {
39
40void handle_FPE (int)
41{
42 // if we get here (presumably in response to SIGFPE triggered
43 // by a trap in integer arithmetic) verify that numeric_limits
44 // traps is non-zero and successfuly exit the process
45 assert (std::numeric_limits<int>::traps);
46
47 std::exit (0);
48}
49
50} // extern "C"
51
52
53#ifdef _MSC_VER
54 // use Structured Exception Handling to detect arithmetic exceptions
55# define TRY __try
56# define EXCEPT(arg) __except (arg)
57#else
58# define TRY if (1)
59# define EXCEPT(ignore) else if (0)
60#endif // _MSC_VER
61
62
63int main ()
64{
65 // prevent clever optimizers from figuring out that (zero == 0)
66 digits [4] = '0';
67 const int zero = (int)std::strtod (digits, 0);
68
69 // compute a non-zero integer value
70 digits [4] = '1';
71 const int non_zero = (int)std::strtod (digits, 0);
72
73 // set up a handler for the FPE signal only when traps is true
74 // otherwise expect to be able to perform integer arithmetic
75 // without a signal
76 if (std::numeric_limits<int>::traps)
77 std::signal (SIGFPE, handle_FPE);
78
79 bool trapped = false;
80
81 // if this traps (generates SIGFPE), verify (in the signal handler)
82 // that integer arithmetic is expected to trap. if this doesn't trap
83 // then we are either on windows, or traps are disabled.
84 TRY {
85 result = non_zero / zero;
86 result += non_zero % zero;
87 }
88 EXCEPT (1) {
89 // windows structured exception caught
90 trapped = true;
91 }
92
93 // if we get this far, verify that we have caught a structured
94 // exception or integer arithmetic is known not to trap
95 assert (trapped == std::numeric_limits<int>::traps);
96
97 (void)&result;
98
99 return 0;
100}
void handle_FPE(int)
volatile int result
#define EXCEPT(ignore)
volatile int zero
Definition LIMITS.cpp:60
#define assert(expr)
Definition _cassert.h:40
#define SIGFPE