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
[-] AlmightySnoo@lemmy.world -2 points 1 year ago* (last edited 1 year ago)

I wouldn't say expression templates are a "gimmick". You likely never wrote high-performance numerical programs in C++ which is why you might think so, but they are essential once you start dealing with matrices and want to avoid unnecessary read/writes from/to the RAM, and it would be actually "bad programming" to not use expression templates to fuse computational kernels at compile-time.

[-] lasagna@programming.dev 2 points 1 year ago* (last edited 1 year ago)

You're right. My hpc experience so far has been mostly older fortran or C but particularly fortran. Performance hasn't been an issue in my current C++ codes as for the scale I need them for they run in a matter of seconds which is more than fine in numerical programming.

So I suppose auto fits in well with those templates?

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