diff --git a/test/enable_if_LICENSE b/test/enable_if_LICENSE new file mode 100644 index 0000000..06f4565 --- /dev/null +++ b/test/enable_if_LICENSE @@ -0,0 +1,53 @@ + Software License + + Copyright 2003 © The Trustees of Indiana University. All rights + reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + 1. All redistributions of source code must retain the above copyright + notice, the list of authors in the original source code, this list + of conditions and the disclaimer listed in this license; + 2. All redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the disclaimer + listed in this license in the documentation and/or other materials + provided with the distribution; + 3. Any documentation included with all redistributions must include + the following acknowledgement: + + + This product includes software developed by the Open Systems Laboratory + at Indiana University. For further information contact Andrew Lumsdaine + at lums@osl.iu.edu. + + Alternatively, this acknowledgement may appear in the software itself, + and wherever such third-party acknowledgments normally appear. + + 4. The name Indiana University shall not be used to endorse or promote + products derived from this software without prior written permission + from Indiana University. For written permission, please contact the + Advanced Research and Technology Institute (ARTI) at 1100 Waterway + Blvd., Indianapolis, Indiana 46202. + 5. Products derived from this software may not be called enable_if, nor + may Indiana University appear in their name, without prior written + permission of ARTI. + + + + Indiana University provides no reassurances that the source code + provided does not infringe the patent or any other intellectual + property rights of any other entity. Indiana University disclaims any + liability to any recipient for claims brought by any other entity + based on infringement of intellectual property rights or otherwise. + + LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH + NO WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA + UNIVERSITY GIVES NO WARRANTIES AND MAKES NO REPRESENTATION THAT + SOFTWARE IS FREE OF INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR + OTHER PROPRIETARY RIGHTS. INDIANA UNIVERSITY MAKES NO WARRANTIES THAT + SOFTWARE IS FREE FROM "BUGS", "VIRUSES", "TROJAN HORSES", "TRAP + DOORS", "WORMS", OR OTHER HARMFUL CODE. LICENSEE ASSUMES THE ENTIRE + RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR ASSOCIATED MATERIALS, + AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION GENERATED USING + SOFTWARE. diff --git a/test/enable_if_constructors.cpp b/test/enable_if_constructors.cpp new file mode 100644 index 0000000..31388c7 --- /dev/null +++ b/test/enable_if_constructors.cpp @@ -0,0 +1,44 @@ +// +// Copyright 2003 © The Trustees of Indiana University. All rights +// reserved. +// +// See the file enable_if_LICENSE for licensing conditions. +// +// Authors: Jaakko Järvi (jajarvi@osl.iu.edu) +// Jeremiah Willcock (jewillco@osl.iu.edu) +// Andrew Lumsdaine (lums@osl.iu.edu) +// + +#include + +#include +#include + +using boost::enable_if; +using boost::disable_if; +using boost::is_arithmetic; + +struct container { + bool my_value; + + template + container(const T&, const typename enable_if::value, T>::type * = 0): + my_value(true) {} + + template + container(const T&, const typename disable_if::value, T>::type * = 0): + my_value(false) {} +}; + +int test_main(int, char*[]) +{ + + BOOST_TEST(container(1).my_value); + BOOST_TEST(container(1.0).my_value); + + BOOST_TEST(!container("1").my_value); + BOOST_TEST(!container(static_cast(0)).my_value); + + return 0; +} + diff --git a/test/enable_if_dummy_arg_disambiguation.cpp b/test/enable_if_dummy_arg_disambiguation.cpp new file mode 100644 index 0000000..9cf98b4 --- /dev/null +++ b/test/enable_if_dummy_arg_disambiguation.cpp @@ -0,0 +1,44 @@ +// +// Copyright 2003 © The Trustees of Indiana University. All rights +// reserved. +// +// See the file enable_if_LICENSE for licensing conditions. +// +// Authors: Jaakko Järvi (jajarvi@osl.iu.edu) +// Jeremiah Willcock (jewillco@osl.iu.edu) +// Andrew Lumsdaine (lums@osl.iu.edu) +// + +#include + +#include +#include + +using boost::enable_if; +using boost::is_arithmetic; + +template struct dummy { + dummy(int) {}; +}; + +template +typename enable_if::value, bool>::type +arithmetic_object(T t, dummy<0> = 0) { return true; } + +template +typename enable_if::value, bool>::type +arithmetic_object(T t, dummy<1> = 0) { return false; } + + +int test_main(int, char*[]) +{ + + BOOST_TEST(arithmetic_object(1)); + BOOST_TEST(arithmetic_object(1.0)); + + BOOST_TEST(!arithmetic_object("1")); + BOOST_TEST(!arithmetic_object(static_cast(0))); + + return 0; +} + diff --git a/test/enable_if_lazy.cpp b/test/enable_if_lazy.cpp new file mode 100644 index 0000000..bdafffd --- /dev/null +++ b/test/enable_if_lazy.cpp @@ -0,0 +1,74 @@ +// +// Copyright 2003 © The Trustees of Indiana University. All rights +// reserved. +// +// See the file enable_if_LICENSE for licensing conditions. +// +// Authors: Jaakko Järvi (jajarvi@osl.iu.edu) +// Jeremiah Willcock (jewillco@osl.iu.edu) +// Andrew Lumsdaine (lums@osl.iu.edu) +// + +#include + +#include +#include + +using boost::enable_if; +using boost::enable_if_lazy; +using boost::is_arithmetic; + +// This class provides a reduced example of a traits class for computing +// the result of multiplying two types. The exists constant is true when a +// multiplication operator exists between two types, and the type member is +// defined to the return type of this operator. The return type member is +// not defined when a multiplication operator does not exist. + +template +struct mult_traits { + BOOST_STATIC_CONSTANT(bool, exists = false); +}; + +template <> +struct mult_traits { + BOOST_STATIC_CONSTANT(bool, exists = true); + typedef int type; +}; + +template <> +struct mult_traits { + BOOST_STATIC_CONSTANT(bool, exists = true); + typedef double type; +}; + +// Next, a forwarding function mult() is defined. It is enabled only when +// mult_traits::exists is true. The first version, using normal +// enable_if, works with only some compilers (it does not work in g++): + +#if 0 +template +typename enable_if< + mult_traits::exists, + typename mult_traits::type>::type +mult(const T& x, const U& y) {return x * y;} +#endif + +// An improved version that works with more compilers uses enable_if_lazy. +// This template removes compiler errors from invalid code used as an +// argument to enable_if. + +template +typename enable_if_lazy< + mult_traits::exists, + mult_traits >::type +mult(const T& x, const U& y) {return x * y;} + +int test_main(int, char*[]) +{ + + BOOST_TEST(mult(1, 2) == 2); + BOOST_TEST(mult(1.0, 3.0) == 3.0); + + return 0; +} + diff --git a/test/enable_if_member_templates.cpp b/test/enable_if_member_templates.cpp new file mode 100644 index 0000000..0f1de59 --- /dev/null +++ b/test/enable_if_member_templates.cpp @@ -0,0 +1,42 @@ +// +// Copyright 2003 © The Trustees of Indiana University. All rights +// reserved. +// +// See the file enable_if_LICENSE for licensing conditions. +// +// Authors: Jaakko Järvi (jajarvi@osl.iu.edu) +// Jeremiah Willcock (jewillco@osl.iu.edu) +// Andrew Lumsdaine (lums@osl.iu.edu) +// + +#include + +#include +#include + +using boost::enable_if; +using boost::disable_if; +using boost::is_arithmetic; + +struct container { + template + typename enable_if::value, bool>::type + arithmetic_object(const T&, const int* /* disambiguate */ = 0) {return true;} + + template + typename disable_if::value, bool>::type + arithmetic_object(const T&) {return false;} +}; + +int test_main(int, char*[]) +{ + + BOOST_TEST(container().arithmetic_object(1)); + BOOST_TEST(container().arithmetic_object(1.0)); + + BOOST_TEST(!container().arithmetic_object("1")); + BOOST_TEST(!container().arithmetic_object(static_cast(0))); + + return 0; +} + diff --git a/test/enable_if_namespace_disambiguation.cpp b/test/enable_if_namespace_disambiguation.cpp new file mode 100644 index 0000000..24bf19e --- /dev/null +++ b/test/enable_if_namespace_disambiguation.cpp @@ -0,0 +1,44 @@ +// +// Copyright 2003 © The Trustees of Indiana University. All rights +// reserved. +// +// See the file enable_if_LICENSE for licensing conditions. +// +// Authors: Jaakko Järvi (jajarvi@osl.iu.edu) +// Jeremiah Willcock (jewillco@osl.iu.edu) +// Andrew Lumsdaine (lums@osl.iu.edu) +// + +#include + +#include +#include + +using boost::enable_if; +using boost::is_arithmetic; + +namespace A { + template + typename enable_if::value, bool>::type + arithmetic_object(T t) { return true; } +} + +namespace B { + template + typename enable_if::value, bool>::type + arithmetic_object(T t) { return false; } +} + +int test_main(int, char*[]) +{ + using namespace A; + using namespace B; + BOOST_TEST(arithmetic_object(1)); + BOOST_TEST(arithmetic_object(1.0)); + + BOOST_TEST(!arithmetic_object("1")); + BOOST_TEST(!arithmetic_object(static_cast(0))); + + return 0; +} + diff --git a/test/enable_if_no_disambiguation.cpp b/test/enable_if_no_disambiguation.cpp new file mode 100644 index 0000000..f8df451 --- /dev/null +++ b/test/enable_if_no_disambiguation.cpp @@ -0,0 +1,40 @@ +// +// Copyright 2003 © The Trustees of Indiana University. All rights +// reserved. +// +// See the file enable_if_LICENSE for licensing conditions. +// +// Authors: Jaakko Järvi (jajarvi@osl.iu.edu) +// Jeremiah Willcock (jewillco@osl.iu.edu) +// Andrew Lumsdaine (lums@osl.iu.edu) +// + +#include + +#include +#include + +using boost::enable_if; +using boost::is_arithmetic; + +template +typename enable_if::value, bool>::type +arithmetic_object(T t) { return true; } + +template +typename enable_if::value, bool>::type +arithmetic_object(T t) { return false; } + + +int test_main(int, char*[]) +{ + + BOOST_TEST(arithmetic_object(1)); + BOOST_TEST(arithmetic_object(1.0)); + + BOOST_TEST(!arithmetic_object("1")); + BOOST_TEST(!arithmetic_object(static_cast(0))); + + return 0; +} + diff --git a/test/enable_if_partial_specializations.cpp b/test/enable_if_partial_specializations.cpp new file mode 100644 index 0000000..bce5455 --- /dev/null +++ b/test/enable_if_partial_specializations.cpp @@ -0,0 +1,45 @@ +// +// Copyright 2003 © The Trustees of Indiana University. All rights +// reserved. +// +// See the file enable_if_LICENSE for licensing conditions. +// +// Authors: Jaakko Järvi (jajarvi@osl.iu.edu) +// Jeremiah Willcock (jewillco@osl.iu.edu) +// Andrew Lumsdaine (lums@osl.iu.edu) +// + +#include + +#include +#include + +using boost::enable_if; +using boost::disable_if; +using boost::is_arithmetic; + +template +struct tester; + +template +struct tester::value>::type> { + BOOST_STATIC_CONSTANT(bool, value = true); +}; + +template +struct tester::value>::type> { + BOOST_STATIC_CONSTANT(bool, value = false); +}; + +int test_main(int, char*[]) +{ + + BOOST_TEST(tester::value); + BOOST_TEST(tester::value); + + BOOST_TEST(!tester::value); + BOOST_TEST(!tester::value); + + return 0; +} +