Skip to content

Commit 7b19a6d

Browse files
committed
Fix syntax error parsing unnamed template parameters with a default.
Closes swig#961 Conflicts: Examples/test-suite/cpp17_enable_if_t.i
1 parent 04217d8 commit 7b19a6d

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

CHANGES.current

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
77
Version 4.1.1 (in progress)
88
===========================
99

10+
2022-11-25: wsfulton
11+
#961 Fix syntax error parsing unnamed template parameters with a default value.
12+
1013
2022-11-25: olly
1114
#2447 Fix undefined behaviour in swig's parser when handling
1215
default parameter expressions containing method calls.

Examples/test-suite/cpp17_enable_if_t.i

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,48 @@ template <typename A, typename B, std::enable_if_t<(std::is_integral_v<A> and st
3232

3333
void tester() {
3434
enableif5<int, int>(10, 20);
35+
enableif5(10, 20);
3536
}
3637
%}
3738

3839
// non-type template parameters working well in SWIG, below is a simple workaround as the 3rd parameter is defaulted for enable_if_t (which is just SFINAE to give a nice C++ compiler error)
3940
%template(enableif5) enableif5<int, int, true>; // workaround
41+
42+
%inline %{
43+
// #961 no name for defaulted template parameter
44+
template<typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
45+
void uuu() {}
46+
template<typename T, typename E = std::enable_if_t<std::is_enum<T>::value>>
47+
void uuuE() {}
48+
49+
template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
50+
void vvv() {}
51+
template<typename T, typename E = typename std::enable_if<std::is_floating_point<T>::value>::type>
52+
void vvvE() {}
53+
54+
// More variations of enable_if and enable_if_t
55+
template<typename T, typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
56+
void www() {}
57+
58+
template<typename T, typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
59+
void xxx() {}
60+
61+
enum TestEnum { Enum1 = 1, Enum2 };
62+
struct TestStruct {};
63+
64+
void tester2() {
65+
uuu<TestEnum>();
66+
// uuu<TestStruct>(); // compilation error
67+
uuuE<TestEnum>();
68+
// uuuE<TestStruct>(); // compilation error
69+
vvv<double>();
70+
// vvv<TestStruct>(); // compilation error
71+
vvvE<double>();
72+
// vvvE<TestStruct>(); // compilation error
73+
74+
www<double>();
75+
// www<TestStruct>(); // compilation error
76+
xxx<TestEnum>();
77+
// xxx<TestStruct>(); // compilation error
78+
}
79+
%}

Source/CParse/parser.y

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4488,8 +4488,9 @@ templateparameters : templateparameter templateparameterstail {
44884488
| empty { $$ = 0; }
44894489
;
44904490

4491-
templateparameter : templcpptype {
4491+
templateparameter : templcpptype def_args {
44924492
$$ = NewParmWithoutFileLineInfo(NewString($1), 0);
4493+
Setattr($$, "value", $2.rawval ? $2.rawval : $2.val);
44934494
}
44944495
| parm {
44954496
$$ = $1;

0 commit comments

Comments
 (0)