More lambda support - for optional lambda declarators

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-matevz@13859 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2012-10-04 06:01:38 +00:00
commit 35458b4a5d
3 changed files with 33 additions and 12 deletions

View file

@ -217,14 +217,29 @@ ignores it.</p>
<H3><a name="Cpp0x_Lambda_functions_and_expressions"></a>7.2.8 Lambda functions and expressions</H3> <H3><a name="Cpp0x_Lambda_functions_and_expressions"></a>7.2.8 Lambda functions and expressions</H3>
<p>SWIG correctly parses the Lambda functions syntax. For example:</p> <p>SWIG correctly parses most of the Lambda functions syntax. For example:</p>
<div class="code"><pre> <div class="code"><pre>
auto val = [] { return something; };
auto sum = [](int x, int y) { return x+y; };
auto sum = [](int x, int y) -&gt; int { return x+y; }; auto sum = [](int x, int y) -&gt; int { return x+y; };
</pre></div> </pre></div>
<p>The lambda functions are removed from the wrapper class for now, because of the lack of support <p>The lambda functions are removed from the wrapper class for now, because of the lack of support
for closures (scope of the lambda functions) in the target languages.</p> for closures (scope of the lambda functions) in the target languages.</p>
<p>
Lambda functions used to create variables can also be parsed, but due to limited support of <tt>auto</tt> when
the type is deduced from the expression, the variables are simply ignored.
</p>
<div class="code"><pre>
auto six = [](int x, int y) { return x+y; }(4, 2);
</pre></div>
<p>
Better support should be available in a later release.
</p>
<H3><a name="Cpp0x_Alternate_function_syntax"></a>7.2.9 Alternate function syntax</H3> <H3><a name="Cpp0x_Alternate_function_syntax"></a>7.2.9 Alternate function syntax</H3>

View file

@ -21,14 +21,14 @@ auto lambda4 = [=](int x, int y) { return x+y; };
int thing = 0; int thing = 0;
#ifdef SWIG #ifdef SWIG
// Not strictly correct as captured variables should have non-automatic storage duration, ie shouldn't capture globals. gcc-4.7 warns about this, but we check that SWIG can parse this anyway. // Not strictly correct as captured variables should have non-automatic storage duration, ie shouldn't capture globals. gcc-4.7 warns about this, but we check that SWIG can parse this anyway.
auto lambda5 = [=,&thing]() { return thing;}; auto lambda5 = [=,&thing]() { return thing; };
#else #else
auto lambda5 = [=]() { return thing;}; auto lambda5 = [=]() { return thing; };
#endif #endif
void fn() { void fn() {
int stuff = 0; int stuff = 0;
auto lambdaxxxx = [=,&stuff]() { return thing;}; auto lambdaxxxx = [=,&stuff]() { return thing; };
} }
auto lambda6 = [] (int a, int b) mutable { return a + b; }; auto lambda6 = [] (int a, int b) mutable { return a + b; };
auto lambda7 = [] (int x, int y) -> int { return x+y; }; auto lambda7 = [] (int x, int y) -> int { return x+y; };
@ -40,6 +40,8 @@ auto lambda12 = [] (int a, int b) { return a + b; }(1, 2);
auto lambda13 = [] (int a, int b) mutable { return a + b; }(1, 2); auto lambda13 = [] (int a, int b) mutable { return a + b; }(1, 2);
auto lambda14 = [] () throw () {}; auto lambda14 = [] () throw () {};
auto lambda15 = [] () mutable throw () {}; auto lambda15 = [] () mutable throw () {};
auto lambda16 = [] { return thing; };
auto lambda17 = [] { return thing; }();
int runLambda1() { int runLambda1() {
return lambda1(5,6); return lambda1(5,6);
@ -72,10 +74,9 @@ int runLambdaInline() {
%{ %{
// TODO // TODO
struct LambdaStruct { struct LambdaStruct {
static constexpr auto lambda_struct1 = [=]() { return thing;}; static constexpr auto lambda_struct1 = [=]() { return thing; };
}; };
int(*lambda101notauto)(int, int) = [] (int a, int b) { return a + b; }; int(*lambda101notauto)(int, int) = [] (int a, int b) { return a + b; };
auto lambda100 = [] { return thing;};
int lambda102 = [] (int a, int b) mutable { return a + b; }(1, 2); int lambda102 = [] (int a, int b) mutable { return a + b; }(1, 2);
void lambda_init(int = ([=]{ return 0; })()); void lambda_init(int = ([=]{ return 0; })());
%} %}

View file

@ -3292,18 +3292,23 @@ cpp_alternate_rettype : primitive_type { $$ = $1; }
| decltype { $$ = $1; } | decltype { $$ = $1; }
; ;
/* /* ------------------------------------------------------------
Lambda functions and expressions, such as: Lambda functions and expressions, such as:
auto myFunc = [](int x, int y) -> int { return x+y; }; auto myFunc = [] { return something; };
auto myFunc = [](int x, int y) { return x+y; }; auto myFunc = [](int x, int y) { return x+y; };
auto myFunc = [](int x, int y) { return x+y; }(1, 2); auto myFunc = [](int x, int y) -> int { return x+y; };
*/ auto myFunc = [](int x, int y) throw() -> int { return x+y; };
auto six = [](int x, int y) { return x+y; }(4, 2);
------------------------------------------------------------ */
cpp_lambda_decl : storage_class AUTO idcolon EQUAL lambda_introducer LPAREN parms RPAREN cpp_const lambda_body lambda_tail { cpp_lambda_decl : storage_class AUTO idcolon EQUAL lambda_introducer LPAREN parms RPAREN cpp_const lambda_body lambda_tail {
$$ = 0; $$ = 0;
} }
| storage_class AUTO idcolon EQUAL lambda_introducer LPAREN parms RPAREN cpp_const ARROW type lambda_body lambda_tail { | storage_class AUTO idcolon EQUAL lambda_introducer LPAREN parms RPAREN cpp_const ARROW type lambda_body lambda_tail {
$$ = 0; $$ = 0;
} }
| storage_class AUTO idcolon EQUAL lambda_introducer lambda_body lambda_tail {
$$ = 0;
}
; ;
lambda_introducer : LBRACKET { lambda_introducer : LBRACKET {