Slightly better decltype() support for expressions

decltype now accepts C++ expressions instead of just an ID, such as:

  int i,j;
  ...  decltype(i+j) ...
  ...  decltype(&i) ...

These result in a warning for non-trivial expressions which SWIG cannot evaluate:

  Warning 344: Unable to deduce decltype for 'i+j'.

See 'Type Inference' in CPlusPlus.html for workarounds.

Issue #1589
Issue #1590
This commit is contained in:
William S Fulton 2022-11-26 01:16:20 +00:00
commit 2a1711e436
8 changed files with 85 additions and 14 deletions

View file

@ -624,6 +624,7 @@ CPP11_TEST_CASES += \
cpp11_thread_local \
cpp11_template_double_brackets \
cpp11_template_explicit \
cpp11_template_parameters_decltype \
cpp11_template_typedefs \
cpp11_type_traits \
cpp11_type_aliasing \

View file

@ -9,11 +9,35 @@
int i;
decltype(i) j;
auto foo( decltype(i) a ) -> decltype(i) {
auto get_number(decltype(i) a) -> decltype(i) {
if (a==5)
return 10;
else
return 0;
}
};
%}
%}
// These are ignored as unable to deduce decltype for (i+j)
%ignore B::k;
%ignore B::get_number_sum;
%ignore B::get_number_address;
#pragma SWIG nowarn=SWIGWARN_CPP11_DECLTYPE
%inline %{
class B {
public:
int i;
decltype(i) j;
decltype(i+j) k;
auto get_number_sum(decltype(i+j) a) -> decltype(i+j) {
return i+j;
}
auto get_number_address(decltype(&i) a) -> decltype(&i) {
return &i;
}
};
%}

View file

@ -9,10 +9,10 @@ a.j = 10
if a.j != 10:
raise RuntimeError("Assignment to a.j failed.")
b = a.foo(5)
b = a.get_int(5)
if b != 10:
raise RuntimeError("foo(5) should return 10.")
raise RuntimeError("get_int(5) should return 10.")
b = a.foo(6)
b = a.get_int(6)
if b != 0:
raise RuntimeError("foo(6) should return 0.")
raise RuntimeError("get_int(6) should return 0.")