News for hackers. Stuff that matters.
Re:Ask HN: C++ devs who went C only by pubby on Sunday April 14, 2024 @06:23PM (#40035007)
I've seen a few people/companies do this and it always baffles me. You don't have to use every C++ feature in existence, but a little bit here and there is far more productive and safe than writing C.
Re:Ask HN: C++ devs who went C only by metadat on Sunday April 14, 2024 @06:39PM (#40035126)
How is it safer? Because of destructors?
Re:Ask HN: C++ devs who went C only by pubby on Sunday April 14, 2024 @07:14PM (#40035358)
Take strings for example. In C you have to manually manage memory and there are a million pitfalls. Lots of programmers are lazy and use fixed-sized arrays and "unsafe" functions like strcpy which results in security vulnerabilities and large-input bugs. But even if you do it correctly you have all this mental overhead and lots of lines of code.

In C++ you just drop in std::string and go.

Re:Ask HN: C++ devs who went C only by farseer on Monday April 15, 2024 @02:45AM (#40037650)
No because of the current active community around C++. The large standard template library is safer to use than in-house implementation of the same data structures. The C++ Boost library also gives you a large arsenal of utility functions that is vetted by the community.
Re:Ask HN: C++ devs who went C only by tsegratis on Sunday April 14, 2024 @06:32PM (#40035076)
Question from a dummy: could you list those features?

Would help me a lot. Link to webpages fine too

Re:Ask HN: C++ devs who went C only by dswilkerson on Sunday April 14, 2024 @08:00PM (#40035657)
Think in object orientation: Put related nouns and verbs together: it is much easier to organize your thoughts using constructors and methods. However do not use virtual methods unless you really must, and if you do, make the class a well-defined explicit interface having only abstract methods. That said, avoid using implementation inheritance: overriding one defined method for another.

Get rid of pointers: When passing objects by reference, using references instead of pointers, especially reference to const, makes code much easier to read. When combined with vector instead of raw arrays, most uses of raw pointers go away.

Do not implement your own fancy data structures: instead use the Standard Template Library. I use STL map, set, and vector all of the time and they remove the need for most other data-structures. Also, when you can, use iterators to traverse these containers, rather than the visitor pattern. Using iterators keeps the control flow of the client contiguous, so it is much more flexible than visitors.

Re:Ask HN: C++ devs who went C only by charleslmunger on Wednesday April 17, 2024 @10:16AM (#40064955)
>Do not implement your own fancy data structures: instead use the Standard Template Library. I use STL map, set, and vector all of the time and they remove the need for most other data-structures.

Don't the STL tree and map data structures have notoriously poor performance?

Re:Ask HN: C++ devs who went C only by roeles on Wednesday April 17, 2024 @10:50AM (#40065476)
I don't think you can make general statements about STL. I have seen a Pi4 outperform a core i7 in very specific cases. That was really unexpected.

In other cases I saw a lot of runtime spent on std::map.

I think the best advice is: learn to use a profiler.

Re:Ask HN: C++ devs who went C only by tsegratis on Thursday April 18, 2024 @08:41PM (#40082276)
From memory, std::map is comparatively slow because the spec requires pointers to indices to be stable. In practice it is slow; see khash and many others

Personally i don't think this matters too much; you're using c/c++; you're already fast

Re:Ask HN: C++ devs who went C only by pubby on Sunday April 14, 2024 @07:11PM (#40035339)
I mean it depends on the project and scope, but if you just write C-style code but use unique_ptr, vector, string, and <algorithm>, you're already at an advantage.
Re:Ask HN: C++ devs who went C only by hnthrowaway0328 on Tuesday April 16, 2024 @03:54PM (#40056428)
Yeah and auto. Unique pointers really make one think about ownership.
Re:Ask HN: C++ devs who went C only by roeles on Wednesday April 17, 2024 @10:51AM (#40065497)
I would recommend replacing macros with constexpr to allow type checking.
Re:Ask HN: C++ devs who went C only by kazinator on Monday April 15, 2024 @02:08PM (#40043795)
You do, if you have to work with an existing code base in which every feature in existence was used.

Even if you start a new greenfield project and get to impose your own coding convention that includes what C++ features should be used, it won't stay that way when people come on board who have different ideas.