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