Support deducing decltype for false and true

This commit is contained in:
Olly Betts 2023-05-20 09:09:48 +12:00
parent 4e4369a4a4
commit d8887091fe
6 changed files with 51 additions and 18 deletions

View File

@ -1,12 +1,12 @@
Below are the changes for the current release.
See the CHANGES file for changes in older releases.
See the RELEASENOTES file for a summary of changes in each release.
Issue # numbers mentioned below can be found on Github. For more details, add
the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.2.0 (in progress)
===========================
2023-05-20: olly
Support deducing the type for `decltype(false)` and
`decltype(true)`.
2023-05-19: olly
#2446 Add support for auto without trailing return type, which is a
C++14 feature.

View File

@ -720,9 +720,9 @@ AltStruct var2{2, 4.3}; // calls the constructor
<H3><a name="CPlusPlus11_type_inference">7.2.6 Type inference</a></H3>
<p>SWIG supports <tt>decltype()</tt> with limitations. Single
variables are allowed, however, non-trivial expressions are not supported very well. For
example, the following code will work:</p>
<p><tt>decltype()</tt> is supported with limitations. SWIG can parse
all uses, but can only deduce the type for single variables, <tt>true</tt> and
<tt>false</tt>. For example, the following code will work:</p>
<div class="code"><pre>
int i;
decltype(i) j;

View File

@ -26,11 +26,14 @@
#pragma SWIG nowarn=SWIGWARN_CPP11_DECLTYPE
%inline %{
#define DECLARE(VAR, VAL) decltype(VAL) VAR = VAL
class B {
public:
int i;
decltype(i) j;
decltype(i+j) k;
DECLARE(a, false);
DECLARE(b, true);
auto get_number_sum(decltype(i+j) a) -> decltype(i+j) {
return i+j;
@ -39,5 +42,9 @@
auto get_number_address(decltype(&i) a) -> decltype(&i) {
return &i;
}
auto negate(decltype(true) b) -> decltype(b) {
return !b;
}
};
%}

View File

@ -16,8 +16,18 @@ check::equal($a->i, 5, 'Assignment to $a->i failed.');
$a->j = 10;
check::equal($a->j, 10, 'Assignment to $a->j failed.');
$b = $a->get_number(5);
check::equal($b, 10, 'get_number(5) should return 10.');
$n = $a->get_number(5);
check::equal($n, 10, 'get_number(5) should return 10.');
$b = $a->get_number(6);
check::equal($b, 0, 'get_number(6) should return 0.');
$n = $a->get_number(6);
check::equal($n, 0, 'get_number(6) should return 0.');
$b = new B();
check::equal($b->a, false);
check::equal($b->b, true);
check::equal($b->negate(true), false);
check::equal($b->negate(false), true);

View File

@ -9,10 +9,24 @@ a.j = 10
if a.j != 10:
raise RuntimeError("Assignment to a.j failed.")
b = a.get_number(5)
if b != 10:
n = a.get_number(5)
if n != 10:
raise RuntimeError("get_number(5) should return 10.")
b = a.get_number(6)
if b != 0:
n = a.get_number(6)
if n != 0:
raise RuntimeError("get_number(6) should return 0.")
b = cpp11_decltype.B()
if b.a != False:
raise RuntimeError("b.a should be False")
if b.b != True:
raise RuntimeError("b.b should be True")
if b.negate(True) != False:
raise RuntimeError("b.negate(True) should return False")
if b.negate(False) != True:
raise RuntimeError("b.negate(False) should return True")

View File

@ -6253,12 +6253,14 @@ decltype : DECLTYPE LPAREN {
decltypeexpr : expr RPAREN {
Node *n = Swig_symbol_clookup($1.val, 0);
if (!n) {
if (n) {
$$ = Getattr(n, "type");
} else if (Equal($1.val, "true") || Equal($1.val, "false")) {
$$ = NewString("bool");
} else {
Swig_warning(WARN_CPP11_DECLTYPE, cparse_file, cparse_line, "Unable to deduce decltype for '%s'.\n", $1.val);
$$ = NewStringf("decltype(%s)", $1.val);
} else {
$$ = Getattr(n, "type");
}
}
| error RPAREN {