mirror of https://github.com/swig/swig
C++11 result_of testcase
This commit is contained in:
parent
a542c5277d
commit
01ce992f5d
|
@ -511,6 +511,7 @@ CPP11_TEST_CASES = \
|
|||
cpp11_noexcept \
|
||||
cpp11_null_pointer_constant \
|
||||
cpp11_raw_string_literals \
|
||||
cpp11_result_of \
|
||||
cpp11_rvalue_reference \
|
||||
cpp11_rvalue_reference2 \
|
||||
cpp11_rvalue_reference3 \
|
||||
|
@ -527,7 +528,6 @@ CPP11_TEST_CASES = \
|
|||
# Broken C++11 test cases.
|
||||
CPP11_TEST_BROKEN = \
|
||||
# cpp11_hash_tables \ # not fully implemented yet
|
||||
# cpp11_result_of \ # SWIG does not support
|
||||
# cpp11_strongly_typed_enumerations \ # SWIG not quite getting this right yet in all langs
|
||||
# cpp11_variadic_templates \ # Broken for some languages (such as Java)
|
||||
# cpp11_reference_wrapper \ # No typemaps
|
||||
|
|
|
@ -4,7 +4,21 @@
|
|||
|
||||
%inline %{
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
typedef double(*fn_ptr)(double);
|
||||
%}
|
||||
|
||||
namespace std {
|
||||
// Forward declaration of result_of
|
||||
template<typename Func> struct result_of;
|
||||
// Add in the required partial specialization of result_of
|
||||
template<> struct result_of< fn_ptr(double) > {
|
||||
typedef double type;
|
||||
};
|
||||
}
|
||||
|
||||
%template() std::result_of< fn_ptr(double) >;
|
||||
|
||||
%inline %{
|
||||
|
||||
double square(double x) {
|
||||
return (x * x);
|
||||
|
@ -14,7 +28,30 @@ template<class Fun, class Arg>
|
|||
typename std::result_of<Fun(Arg)>::type test_result_impl(Fun fun, Arg arg) {
|
||||
return fun(arg);
|
||||
}
|
||||
|
||||
std::result_of< fn_ptr(double) >::type test_result_alternative1(double(*fun)(double), double arg) {
|
||||
return fun(arg);
|
||||
}
|
||||
%}
|
||||
|
||||
%template(test_result) test_result_impl<double(*)(double), double>;
|
||||
%{
|
||||
// Another alternative approach using decltype (not very SWIG friendly)
|
||||
std::result_of< decltype(square)&(double) >::type test_result_alternative2(double(*fun)(double), double arg) {
|
||||
return fun(arg);
|
||||
}
|
||||
%}
|
||||
|
||||
%inline %{
|
||||
#include <iostream>
|
||||
|
||||
void cpp_testing() {
|
||||
std::cout << "result: " << test_result_impl(square, 3) << std::endl;
|
||||
std::cout << "result: " << test_result_impl<double(*)(double), double>(square, 4) << std::endl;
|
||||
std::cout << "result: " << test_result_impl< fn_ptr, double >(square, 5) << std::endl;
|
||||
std::cout << "result: " << test_result_alternative1(square, 6) << std::endl;
|
||||
std::cout << "result: " << test_result_alternative2(square, 7) << std::endl;
|
||||
}
|
||||
%}
|
||||
|
||||
%template(test_result) test_result_impl< fn_ptr, double>;
|
||||
%constant double (*SQUARE)(double) = square;
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
import cpp11_result_of.*;
|
||||
|
||||
public class cpp11_result_of_runme {
|
||||
|
||||
static {
|
||||
try {
|
||||
System.loadLibrary("cpp11_result_of");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String argv[])
|
||||
{
|
||||
double result = cpp11_result_of.test_result(cpp11_result_ofConstants.SQUARE, 3.0);
|
||||
if (result != 9.0)
|
||||
throw new RuntimeException("test_result(square, 3.0) is not 9.0. Got: " + Double.toString(result));
|
||||
|
||||
result = cpp11_result_of.test_result_alternative1(cpp11_result_ofConstants.SQUARE, 3.0);
|
||||
if (result != 9.0)
|
||||
throw new RuntimeException("test_result_alternative1(square, 3.0) is not 9.0. Got: " + Double.toString(result));
|
||||
}
|
||||
}
|
|
@ -1,3 +1,9 @@
|
|||
import cpp11_result_of
|
||||
if cpp11_result_of.test_result(cpp11_result_of.square, 3.0) != 9.0:
|
||||
raise RuntimeError, "test_result(square, 3.0) is not 9.0."
|
||||
|
||||
result = cpp11_result_of.test_result(cpp11_result_of.SQUARE, 3.0)
|
||||
if result != 9.0:
|
||||
raise RuntimeError, "test_result(square, 3.0) is not 9.0. Got: " + str(result)
|
||||
|
||||
result = cpp11_result_of.test_result_alternative1(cpp11_result_of.SQUARE, 3.0)
|
||||
if result != 9.0:
|
||||
raise RuntimeError, "test_result_alternative1(square, 3.0) is not 9.0. Got: " + str(result)
|
||||
|
|
Loading…
Reference in New Issue