diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2014-03-14 01:53:53 +0000 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2014-03-14 01:57:16 +0000 |
commit | e5f928e97ad05aa4c87e92e6c5b53813c204db11 (patch) | |
tree | ee63524712190591f80602ec1f0f9ff632636ae9 | |
parent | 01ce992f5d8ca20beffbc91be3fffe412277c5eb (diff) | |
download | swig-e5f928e97ad05aa4c87e92e6c5b53813c204db11.tar.gz |
Add metaprogramming type_traits example in C++11 documentation
-rw-r--r-- | Examples/test-suite/common.mk | 1 | ||||
-rw-r--r-- | Examples/test-suite/cpp11_type_traits.i | 29 | ||||
-rw-r--r-- | Examples/test-suite/python/cpp11_type_traits_runme.py | 7 |
3 files changed, 37 insertions, 0 deletions
diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index e3543a78a..9a05c531d 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -521,6 +521,7 @@ CPP11_TEST_CASES = \ cpp11_template_double_brackets \ cpp11_template_explicit \ cpp11_template_typedefs \ + cpp11_type_traits \ cpp11_uniform_initialization \ cpp11_unrestricted_unions \ cpp11_userdefined_literals \ diff --git a/Examples/test-suite/cpp11_type_traits.i b/Examples/test-suite/cpp11_type_traits.i new file mode 100644 index 000000000..715ce99fa --- /dev/null +++ b/Examples/test-suite/cpp11_type_traits.i @@ -0,0 +1,29 @@ +%module cpp11_type_traits + +// The example in the CPlusPlus11.html documentation. +// This doesn't really directly test functionality in type_traits as it doesn't provide +// much for use by target languages, rather it tests usage of it. + +%inline %{ +#include <type_traits> + +// First way of operating. +template< bool B > struct algorithm { + template< class T1, class T2 > static int do_it(T1 &, T2 &) { /*...*/ return 1; } +}; + +// Second way of operating. +template<> struct algorithm<true> { + template< class T1, class T2 > static int do_it(T1, T2) { /*...*/ return 2; } +}; + +// Instantiating 'elaborate' will automatically instantiate the correct way to operate, depending on the types used. +template< class T1, class T2 > int elaborate(T1 A, T2 B) { + // Use the second way only if 'T1' is an integer and if 'T2' is + // a floating point, otherwise use the first way. + return algorithm< std::is_integral<T1>::value && std::is_floating_point<T2>::value >::do_it(A, B); +} +%} + +%template(Elaborate) elaborate<int, int>; +%template(Elaborate) elaborate<int, double>; diff --git a/Examples/test-suite/python/cpp11_type_traits_runme.py b/Examples/test-suite/python/cpp11_type_traits_runme.py new file mode 100644 index 000000000..cb58656a6 --- /dev/null +++ b/Examples/test-suite/python/cpp11_type_traits_runme.py @@ -0,0 +1,7 @@ +from cpp11_type_traits import * + +if Elaborate(0, 0) != 1: + raise RuntimeError("Elaborate should have returned 1") + +if Elaborate(0, 0.0) != 2: + raise RuntimeError("Elaborate should have returned 2") |