this post was submitted on 15 Jul 2023
11 points (100.0% liked)

C++

1777 readers
1 users here now

The center for all discussion and news regarding C++.

Rules

founded 1 year ago
MODERATORS
 

Since c++11 it has been possible that instead of declaring your function as "int name(arguments);" you can now do "auto name(arguments) -> int;". The place I work at has it as style rule that all functions need to be declared that way. Now obviously this is not that large of a thing, and a consistent style is more important than my opinion here. But this has always felt like a weird thing, adding extra bloat to reading code. Anyways looking around I saw some positives to this construction, generally with the use of long return types, that are paramount when using templates. Here the benefit is that the function name is not hidden behind multiple template declarations, which does seem like a good argument. Also lamndbas generally use this. However I personally see some negatives here with using this foe every function, namely:

  • extra bloat when typing/reading the code. This however could be automated to switch between the needed representations. It currently isent so I personally have ti type the auto and trailing return type manually, its not a lot, but still. Also reading code has become a bit more annoying if you have a lot of function overrides as you now have to first look which block of declarations have the smae function name and then parse which one has the correct return type.
  • inconsistency with other typed programming languages: This one is probably why it irks me, but (and correct me on this) I dont know of a c style typed programming language that supports this type of syntax. Python has typehints(which you should use, please), which are declared after the function, and I remember Haskell also has their return type after the function name. But both of thede languages serve a different function than c++. More similar languages like c# and java dont support trailing return types.

Anyway enough of me ranting, I would like to know wath the other opinions here are. And whether this rant is missing se important arguments?

you are viewing a single comment's thread
view the rest of the comments
[–] gracicot 1 points 1 year ago

I use trailing return type exclusively. It just makes the code more readable. Compactness is almost unaffected and readability is more important anyway. Trailing return type is also more compact in many normal cases non temaplate case so I think that argument is moot.

The name of the function is the first thing you want to read. The most important thing once you found your function is then it's parameters. If you found the right overload, you know the parameters and what they mean then you want to know what it returns.

Trailing return type just have better ergonomics for the reader and also align the functions as a bonus. It so make name resolution better too.