Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Compiler_Features_13_Test.cpp
blob15308dc8af317c4db50fc52db42e20279001c8fa
1 /**
2 * This program checks if the compiler / platform supports the
3 * standard cast operators template parameters. The motivation for
4 * this test was a discussion on the development mailing list, and the
5 * documentation was captured in:
7 * http://bugzilla.dre.vanderbilt.edu/show_bug.cgi?id=3715
8 */
10 #include "test_config.h"
12 #include <stdexcept>
13 #include <typeinfo>
15 namespace
17 /**
18 * Helper class for test
20 struct Base
22 virtual ~Base()
26 /**
27 * Helper class for test
29 struct Derived : public Base
31 int value;
34 /**
35 * Helper class for test
37 struct Another : public Base
39 int x;
40 int y;
44 int
45 run_main (int, ACE_TCHAR *[])
47 ACE_START_TEST (ACE_TEXT("Compiler_Features_13_Test"));
49 // As usual, the exit status from the test is 0 on success, 1 on
50 // failure
51 int status = 0;
54 // Make sure const cast works. Compilation is interesting, the
55 // functionality test here is just to make sure the compiler does
56 // not optimize things away ...
57 int x = 5;
58 int const & y = x;
59 const_cast<int&>(y) = 3;
61 if (x != 3)
63 status = 1;
64 ACE_ERROR((LM_ERROR,
65 ACE_TEXT("Wrong value after const_cast,")
66 ACE_TEXT(" expected %d, got %d\n"),
67 3, x));
71 // Make sure dynamic cast through pointers work ...
72 Derived d;
73 d.value = 24;
74 Base * b1 = &d;
75 Derived * d1 = dynamic_cast<Derived*>(b1);
76 if (d1 == 0)
78 status = 1;
79 ACE_ERROR((LM_ERROR,
80 ACE_TEXT("dynamic_cast returns null, expected value\n")));
82 else
84 d1->value = 42;
85 if (d.value != 42)
87 ACE_ERROR((LM_ERROR,
88 ACE_TEXT("Wrong value after dynamic_cast, expected %d, got %d\n"),
89 42, d.value));
93 // Make sure dynamic cast detects invalid casts
94 Another a;
95 Base * b2 = &a;
96 Derived * d2 = dynamic_cast<Derived*>(b2);
97 if (d2 != 0)
99 status = 1;
100 ACE_ERROR((LM_ERROR,
101 ACE_TEXT("dynamic_cast should return null\n")));
104 // Make sure dynamic cast raises a bad_cast exception
105 Base & b3 = a;
108 (void) dynamic_cast<Derived&>(b3);
110 status = 1;
111 ACE_ERROR((LM_ERROR,
112 ACE_TEXT("dynamic_cast should have raised std::bad_cast\n")));
114 catch(std::bad_cast const &)
117 catch(...)
119 status = 1;
120 ACE_ERROR((LM_ERROR,
121 ACE_TEXT("dynamic_cast should have raised std::bad_cast\n")));
125 // Just test these compile ...
126 double x = 42.0;
127 int y = static_cast<int>(x);
128 void * z = reinterpret_cast<void*>(y);
130 if (z == 0)
132 ACE_ERROR((LM_ERROR,
133 ACE_TEXT("My hack to make sure the code is not ")
134 ACE_TEXT("optimized away backfired!\n")));
138 ACE_END_TEST;
139 return status;