Short-form for function and/or lambda bodies.

168 views
Skip to first unread message

andrew...@gmail.com

unread,
Jul 9, 2013, 2:47:42 PM7/9/13
to std-pr...@isocpp.org

As an alternative short-form for defining a function:

    function-body:
        ... // existing bodies
        = expression ;
        = braced-init-list

Where such a form is equivalent to { return expression; } and { return braced-init-list; } respectively, except where a pure virtual specifier (= 0) would not be ill-formed.  (In cases where `= 0` is well-formed parenthesis can be used to disambiguate `= (0)`)

So:

    int f(int a, int b) = a + b;

is the same as:

    int f(int a, int b) { return a + b; }

and

    vector<int> g(int a, int b) = {a, b}

is the same as:

    vector<int> g(int a, int b) { return {a, b}; }

Likewise for lambdas maybe:

    lambda-expression:
        lambda-introducer lambda-declarator_opt lambda-body

    lambda-body:
        compound-statement
        = expression
        = braced-init-list

Some thought would have to be given to termination of the `= expression` case.  Perhaps it can greedily match the longest expression.

But the general idea is the same:

    foo([] = a + b);

is the same as:

    foo([] { return a + b; });

(Inspired from Scala, and the mostly empty syntactic space after function-declarator=) .

Enjoy,
Andrew.

sghe...@gmail.com

unread,
Jul 9, 2013, 4:38:27 PM7/9/13
to std-pr...@isocpp.org, andrew...@gmail.com


On Tuesday, July 9, 2013 8:47:42 PM UTC+2, andrew...@gmail.com wrote:

As an alternative short-form for defining a function:

You do realize this is C++, and having too few alternative syntax construct is not one of the main language impediments? 
Reply all
Reply to author
Forward
0 new messages