Make x.0.10 publicly available
[ACE_TAO.git] / ACE / tests / Compiler_Features_14_Test.cpp
blobe68def2a0ac641a71b6c48264b0ea627b0f4c2bc
1 /**
2 * @file
4 * This program checks if the compiler / platform supports partial
5 * template specialization. The motivation for this test was a
6 * discussion on the development mailing list, and the documentation
7 * was captured in:
9 * http://bugzilla.dre.vanderbilt.edu/show_bug.cgi?id=3715
12 #include "test_config.h"
14 // We are going to test if partial template specializations work by
15 // demonstrating a standard technique in generic programming, i.e.,
16 // using the specialization to detect if a type is a pointer.
18 // My implementation here is not very elegant, I would even say
19 // ackward, and should not be taken as representative of good generic
20 // programming techniques. I just wanted to through something
21 // together.
24 // First some helper types in the anonymous namespace
25 struct true_type {};
26 struct false_type {};
28 // Now a generic function to convert the types to booleans, moving
29 // from generic type-based programming to classical value-based
30 // programming.
31 template<class T>
32 bool to_boolean(T const&);
34 template<>
35 bool to_boolean(true_type const &)
37 return true;
40 template<>
41 bool to_boolean(false_type const &)
43 return false;
46 // Here is the template, by default return false for all types.
47 // Notice that this is a type *function*, it takes a type and returns
48 // another type.
49 template<typename T>
50 struct is_pointer_function
52 false_type result;
55 // Here is the specialization, for a class of types it results
56 // something different. Effectively this is an implicit if() test on
57 // the types.
58 template<typename T>
59 struct is_pointer_function<T*>
61 true_type result;
64 // And here is a helper to convert back to values...
65 struct test
67 template<typename T>
68 static bool is_pointer()
70 is_pointer_function<T> v;
71 return to_boolean(v.result);
75 int
76 run_main (int, ACE_TCHAR *[])
78 ACE_START_TEST (ACE_TEXT("Compiler_Features_14_Test"));
80 // As usual, the exit status from the test is 0 on success, 1 on
81 // failure
82 int status = 0;
84 if (test::is_pointer<int>())
86 status = 1;
87 ACE_ERROR((LM_ERROR,
88 ACE_TEXT("int should not be a pointer\n")));
91 if (! test::is_pointer<int*>())
93 status = 1;
94 ACE_ERROR((LM_ERROR,
95 ACE_TEXT("int* should be a pointer\n")));
98 if (test::is_pointer<int&>())
100 status = 1;
101 ACE_ERROR((LM_ERROR,
102 ACE_TEXT("int& should not be a pointer\n")));
105 ACE_END_TEST;
106 return status;