11
submitted 1 year ago by troye888@lemmy.one to c/cpp@programming.dev

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
[-] troye888@lemmy.one 2 points 1 year ago

I have found it nice to use for large types (nested containers, lambdas) which are only used once, and I would not necessarily want a typedef. However I also dont like using it too much its basically trading up coding speed for reading speed. And tile and time again it has been found that the latter one is done a lot more.

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

C++

1718 readers
20 users here now

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

Rules

founded 1 year ago
MODERATORS