[Nfd-dev] C++11 code style
Alex Afanasyev
alexander.afanasyev at ucla.edu
Sun Nov 2 12:57:25 PST 2014
> On Nov 2, 2014, at 12:41 PM, Junxiao Shi <shijunxiao at email.arizona.edu> wrote:
>
> Dear folks
>
> The choice between
> [&capture1, capture2] (Type1 arg1, Type2 arg2)
> {
> statements;
> }
> and
> [&capture1, capture2] (Type1 arg1, Type2 arg2) {
> statements;
> }
> depends on whether a lambda expression is more like a function, or a block statement (similar to if/for/while).
> A lambda expression is an inline function and rule 1.5 mandates the first style for functions.
> Another possible understanding is: put "{" on the next line for everything down to named function level, and put "{" on the same line for everything inside a named function. This makes the second style preferred.
> What do others on nfd-dev think?
Even though lambda function is logically a function, it is used as a block statement. Therefore, the rule (if any) should be closer to a block statement rule, then to function.
> `auto` specifier can be used to declare variables at any scope: block scope, namespace scope, init statement of for loop.
> The proposed rule is to forbid using `auto` specifier at namespace scope, because such usage makes the code less readable.
I would avoid making such an explicit rule. I would rather take case by case approach, where namespace-scope auto should be generally avoided, but if it makes sense to use it, it can be used.
—
Alex
> Yours, Junxiao
>
> On Sun, Nov 2, 2014 at 1:03 PM, Alex Afanasyev <alexander.afanasyev at ucla.edu <mailto:alexander.afanasyev at ucla.edu>> wrote:
>
>> On Nov 2, 2014, at 9:20 AM, Junxiao Shi <shijunxiao at email.arizona.edu <mailto:shijunxiao at email.arizona.edu>> wrote:
>>
>> Dear folks
>>
>> We need to define some new code style rules for C++11 syntax.
>>
>> I'd start with three new rules:
>>
>> rule x. Range-based for should be in the following form:
>>
>> for (auto i : range) {
>> statements;
>> }
>>
>>
>> rule x. Lambda expression should be in the following form:
>>
>> [&capture1, capture2] (Type1 arg1, Type2 arg2)
>> {
>> statements;
>> }
>
> I prefer different style, which more aligns with the existing code (and is more gnu style):
>
> [&capture1, capture2] (Type1 arg1, Type2 arg2) {
> statements;
> }
>
>>
>> If the function body has only one line, and the whole expression can fit in one line, the following form is also acceptable:
>>
>> [&capture1, capture2] (Type1 arg1, Type2 arg2) { statement; }
>
> that’s fine too
>
>>
>> rule x. `auto` type should only be used for local variables.
>
> Where else, potentially, it can be used?
>
>>
>> What other rules do we need?
>>
>> Yours, Junxiao
>> _______________________________________________
>> Nfd-dev mailing list
>> Nfd-dev at lists.cs.ucla.edu <mailto:Nfd-dev at lists.cs.ucla.edu>
>> http://www.lists.cs.ucla.edu/mailman/listinfo/nfd-dev <http://www.lists.cs.ucla.edu/mailman/listinfo/nfd-dev>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.lists.cs.ucla.edu/pipermail/nfd-dev/attachments/20141102/19afd4cb/attachment.html>
More information about the Nfd-dev
mailing list