You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Require '=' for all funcs, default single-expr bodies to -> forward _
Requiring `=` has been a widely-requested change, and I think it does make the code clearer to read. See the Cpp2 code changes in this commit for examples.
For example, `f: () expr;` must now be written as `f: () = expr;`
For a function whose body is a single expression, the default return type (i.e., if not specified) is now `-> forward _`, which is Cpp1 `-> decltype(auto)` (which, importantly, can deduce a value).
With this change, single-expression function bodies without `{ }` are still legal for any function, but as of this commit we have a clearer distinction in their use (which is reflected in the updates to the regression tests and other Cpp2 code in this commit):
- It further encourages single-expression function bodies without `{ }` for unnamed function expressions (lambdas), by making more of those cases Do the Right Thing that the programmer intended.
- It naturally discourages their overuse for named functions, because it will more often cause a compiler warning or error:
- a warning that callers are not using a returned results, when the function is called without using its value
- an error that a deduced return type makes the function order-dependent, when the function is called from earlier in the source file... this is because a deduced return types creates a dependency on the body, and it is inherent (not an artifact of either Cpp1 or Cpp2)
But for those who liked that style, fear not, usually the answer is to just put the two characters `{ }` around the body... see examples in this commit, which I have to admit most are probably more readable even though I'm one of the ones who liked omitting the braces.
Note: I realize that further evolution may not allow the `{ }`-free style for named functions. That may well be a reasonable further outcome.
I think both of these are positive improvements.
Copy file name to clipboardExpand all lines: regression-tests/mixed-is-as-variant.cpp2
+2-2
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
test: (forward v) = {
2
2
std::cout << "v is empty = (v is void)$" << std::endl;
3
3
std::cout << "v is std::monostate = (v is std::monostate)$" << std::endl;
4
-
std::cout << "v is X< 0> = (v is X< 0>)$,\t(v as X< 1>) = " << expect_no_throw(forward v, :(forward v) v as X<0>) << std::endl;
4
+
std::cout << "v is X< 0> = (v is X< 0>)$,\t(v as X< 1>) = " << expect_no_throw(forward v, :(forward v) = v as X<0>) << std::endl;
5
5
std::cout << "v is X< 1> = (v is X< 1>)$,\t(v as X< 1>).to_string() = (expect_no_throw(forward v, :(forward v) -> std::string = { return (v as X< 1>).to_string();}))$" << std::endl;
6
6
std::cout << "v is X<19> = (v is X<19>)$,\t(v as X<19>).to_string() = (expect_no_throw(forward v, :(forward v) -> std::string = { return (v as X<19>).to_string();}))$" << std::endl;
7
-
std::cout << "v is X<20> = (v is X<20>)$,\t(v as X<20>) = " << expect_no_throw(forward v, :(forward v) v as X<20>) << std::endl;
7
+
std::cout << "v is X<20> = (v is X<20>)$,\t(v as X<20>) = " << expect_no_throw(forward v, :(forward v) = v as X<20>) << std::endl;
0 commit comments