From a61a32ac1eceed2f1fc5d063d5280986770076b6 Mon Sep 17 00:00:00 2001 From: coryan Date: Fri, 3 Jul 2009 13:11:16 +0000 Subject: Fri Jul 3 13:10:03 UTC 2009 Carlos O'Ryan * tests/run_test.lst: * tests/Makefile.am: * tests/tests.mpc: * tests/Compiler_Features_14_Test.cpp: This test verifies that partial template specialization works. --- ACE/ChangeLog | 8 +++ ACE/tests/Compiler_Features_14_Test.cpp | 114 ++++++++++++++++++++++++++++++++ ACE/tests/Makefile.am | 20 ++++++ ACE/tests/run_test.lst | 1 + ACE/tests/tests.mpc | 7 ++ 5 files changed, 150 insertions(+) create mode 100644 ACE/tests/Compiler_Features_14_Test.cpp diff --git a/ACE/ChangeLog b/ACE/ChangeLog index f6d1f4455ab..0c5f4aa35c4 100644 --- a/ACE/ChangeLog +++ b/ACE/ChangeLog @@ -1,3 +1,11 @@ +Fri Jul 3 13:10:03 UTC 2009 Carlos O'Ryan + + * tests/run_test.lst: + * tests/Makefile.am: + * tests/tests.mpc: + * tests/Compiler_Features_14_Test.cpp: + This test verifies that partial template specialization works. + Fri Jul 3 12:47:41 UTC 2009 Carlos O'Ryan * tests/Compiler_Features_11_Test.cpp: diff --git a/ACE/tests/Compiler_Features_14_Test.cpp b/ACE/tests/Compiler_Features_14_Test.cpp new file mode 100644 index 00000000000..e95b474887c --- /dev/null +++ b/ACE/tests/Compiler_Features_14_Test.cpp @@ -0,0 +1,114 @@ +// $Id$ + +/** + * @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" + +ACE_RCSID(tests, Compiler_Features_14_Test, "$Id$") + +// 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 +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 +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 +struct is_pointer_function +{ + true_type result; +}; + +// And here is a helper to convert back to values... +struct test +{ + template + static bool is_pointer() + { + is_pointer_function 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()) + { + status = 1; + ACE_ERROR((LM_ERROR, + ACE_TEXT("int should not be a pointer\n"))); + } + + if (! test::is_pointer()) + { + status = 1; + ACE_ERROR((LM_ERROR, + ACE_TEXT("int* should be a pointer\n"))); + } + + if (test::is_pointer()) + { + status = 1; + ACE_ERROR((LM_ERROR, + ACE_TEXT("int& should not be a pointer\n"))); + } + + ACE_END_TEST; + return status; +} + + diff --git a/ACE/tests/Makefile.am b/ACE/tests/Makefile.am index cac688be617..5aae1b38a47 100644 --- a/ACE/tests/Makefile.am +++ b/ACE/tests/Makefile.am @@ -903,6 +903,26 @@ Compiler_Features_13_Test_LDADD = \ endif !BUILD_ACE_FOR_TAO +## Makefile.Compiler_Features_14_Test.am + +if !BUILD_ACE_FOR_TAO + +noinst_PROGRAMS += Compiler_Features_14_Test + +Compiler_Features_14_Test_CPPFLAGS = \ + -I$(ACE_ROOT) \ + -I$(ACE_BUILDDIR) + +Compiler_Features_14_Test_SOURCES = \ + $(ACE_ROOT)/tests/Main.cpp \ + Compiler_Features_14_Test.cpp \ + +Compiler_Features_14_Test_LDADD = \ + libTest_Output.la \ + $(ACE_BUILDDIR)/ace/libACE.la + +endif !BUILD_ACE_FOR_TAO + ## Makefile.Config_Test.am if !BUILD_ACE_FOR_TAO diff --git a/ACE/tests/run_test.lst b/ACE/tests/run_test.lst index d2fb7e471d3..9b531153912 100644 --- a/ACE/tests/run_test.lst +++ b/ACE/tests/run_test.lst @@ -73,6 +73,7 @@ Compiler_Features_09_Test Compiler_Features_11_Test Compiler_Features_12_Test Compiler_Features_13_Test +Compiler_Features_14_Test Config_Test: !LynxOS !VxWorks !ACE_FOR_TAO Conn_Test: !ACE_FOR_TAO DLL_Test: !Unicos !STATIC !KCC_Linux diff --git a/ACE/tests/tests.mpc b/ACE/tests/tests.mpc index b6c115eb9ee..73e4ef50ee2 100644 --- a/ACE/tests/tests.mpc +++ b/ACE/tests/tests.mpc @@ -527,6 +527,13 @@ project(Compiler_Features_13_Test) : acetest { } } +project(Compiler_Features_14_Test) : acetest { + exename = Compiler_Features_14_Test + Source_Files { + Compiler_Features_14_Test.cpp + } +} + project(Config Test) : acetest { avoids += ace_for_tao exename = Config_Test -- cgit v1.2.1