views:

201

answers:

2

Although this "conversation" could quickly degenerate into something like "this is how I think ..." the question is not. Has any "big name entity" (e.g., google or the likes, Scott Meyers or the likes, etc.) published anything freely available which dictates/suggests what they feel the syntax guides lines for their code base should be for some/any of the C++0x features?

For example, I've seen all sort of example code for variadic templates written with different spacing with respect to the ellipses. Has anyone weighed anyone weighed in on what the believe is the most expressive/readable version of some/all the constructs?

@jalf: Even if you don't believe anyone should tell you how to do such trivialities there are examples of big organizations that have formatting related standards in their coding standards (e.g., Google, Gnu) but these don't have to do with C++0x features (which was what I was asking).

+5  A: 

As with all coding style questions, the answer to the question is inevitably "it depends." For example, short lambdas should definitely be one-liners:

std::transform(v.begin(), v.end(), v.begin(), [](int i) { return i + 1; });

Longer lambdas that can't fit easily on one-line should be spread out over multiple lines:

std::transform(v.begin(), v.end(), v.begin(), [](int i) -> int
{ 
    // lines
    // of 
    // code
    return i + 1; 
});

Does it matter whether the starting brace goes on the same line as the call to transform or on its own line? No.

In my opinion, the only good, consistent rule with respect to code formatting is "if it looks ugly, make it look less ugly."

James McNellis
This is completely my own opinion, but the second looks deeply horrible to me. You start with a function call, then somewhere towards the end of the line you introduce what is effectively a function definition. Scanning quickly down the code it's not going to be obvious that the block is part of a lambda. I think that my policy would be that once your lambda has grown to beyond a one-liner it should deserve a descriptive name and should be promoted to a function or function object.
Charles Bailey
@Charles: I don't think it's a problem. I think it's clear that it's part of the `transform`, which is what matters. Whether it is a lambda, or a function call, or anything else, it is in the position where we expect to see the "how to transform" parameter. A couple of lines is fine by me. Of course, a 10-line lambda would be overkill. But yeah, subjective, of course.
jalf
@Charles: I think people who've worked with languages with first-class functions should feel right at home with the 2nd example. I for one am happy C++ users will too, some day :)
Cogwheel - Matthew Orlando
A: 

I imagine there really isn't any need for C++0x extensions even if you did want to enforce a particular style (say, within a given project). Assuming your style guide speaks in general enough terms, there's no reason lambda syntax wouldn't fit right in. A single guideline for inline code blocks should suffice to cover things like inline class methods, lambdas, control statements, etc.

Cogwheel - Matthew Orlando