pybind11/tests/test_constants_and_functions.py
Jason Rhinelander 6e036e78a7 Support binding noexcept function/methods in C++17
When compiling in C++17 mode the noexcept specifier is part of the
function type.  This causes a failure in pybind11 because, by omitting
a noexcept specifier when deducing function return and argument types,
we are implicitly making `noexcept(false)` part of the type.

This means that functions with `noexcept` fail to match the function
templates in cpp_function (and other places), and we get compilation
failure (we end up trying to fit it into the lambda function version,
which fails since a function pointer has no `operator()`).

We can, however, deduce the true/false `B` in noexcept(B), so we don't
need to add a whole other set of overloads, but need to deduce the extra
argument when under C++17.  That will *not* work under pre-C++17,
however.

This commit adds two macros to fix the problem: under C++17 (with the
appropriate feature macro set) they provide an extra `bool NoExceptions`
template argument and provide the `noexcept(NoExceptions)` deduced
specifier.  Under pre-C++17 they expand to nothing.

This is needed to compile pybind11 with gcc7 under -std=c++17.
2016-12-14 20:40:49 +01:00

44 lines
1.1 KiB
Python

def test_constants():
from pybind11_tests import some_constant
assert some_constant == 14
def test_function_overloading():
from pybind11_tests import MyEnum, test_function
assert test_function() == "test_function()"
assert test_function(7) == "test_function(7)"
assert test_function(MyEnum.EFirstEntry) == "test_function(enum=1)"
assert test_function(MyEnum.ESecondEntry) == "test_function(enum=2)"
assert test_function(1, 1.0) == "test_function(int, float)"
assert test_function(2.0, 2) == "test_function(float, int)"
def test_bytes():
from pybind11_tests import return_bytes, print_bytes
assert print_bytes(return_bytes()) == "bytes[1 0 2 0]"
def test_exception_specifiers():
from pybind11_tests.exc_sp import C, f1, f2, f3, f4
c = C()
assert c.m1(2) == 1
assert c.m2(3) == 1
assert c.m3(5) == 2
assert c.m4(7) == 3
assert c.m5(10) == 5
assert c.m6(14) == 8
assert c.m7(20) == 13
assert c.m8(29) == 21
assert f1(33) == 34
assert f2(53) == 55
assert f3(86) == 89
assert f4(140) == 144