r/cpp • • Aug 25 '21

Experience C++ developers: do you enjoy using C++ (even for personal/hobby projects?)

Do you actually enjoy using C++, or do you only use it for work, or purely for performance? This question is aimed at people with years of experience, who've worked on large projects in C++. I see people who dislike the language, but use it purely for performance reasons. This sort of discourages me from learning it. I don't want programming to feel like work.

204 Upvotes

327 comments sorted by

View all comments

Show parent comments

18

u/echidnas_arf Aug 25 '21

I started working on some personal projects using C++ and I am creating classes and objects. How is that bad or not modern?

Nothing wrong with classes, and nothing wrong with an object-oriented approach, if appropriate.

Just be aware that C++ offers other ways of implementing polymorphic behaviour, such as static polymorphism (i.e., templates) and runtime polymorphism based on type erasure which are often more appropriate than classical OO class hierarchies.

1

u/XDracam Aug 26 '21

What do you mean by runtime polymorphism based on type erasure? I hope you don't mean casting to arbitrary pointers...

3

u/kiwitims Aug 26 '21

At a guess they could be talking about std::function, std::variant, or things like folly::poly. I doubt they're recommending going back to void*.

2

u/echidnas_arf Aug 26 '21

I am referring to things like std::function and std::thread. That is, regular (non-template) classes which can wrap objects modelling specific conceptual requirements. Essentially, you can achieve a result similar to inheritance-based polymorphism without having to participate in a class hierarchy and without pointer-based semantics.

Under the hood, such classes can be implemented using classic OO polymorphism (although the user never sees pointers and the type-erased classes themselves provide value semantics), but other approaches are also possible to achieve the same goal.

See here for a library that can help in the creation of such type-erased polymorphic classes:

https://github.com/ldionne/dyno

1

u/XDracam Aug 26 '21

Huh, neat. Thanks! You never stop learning C++

1

u/[deleted] Aug 26 '21

It's enough to make a lot of sane programmers run a mile... It sounds like you need a PhD in compiler fornication to get close to knowing how to design your sw in c++.. "Runtime polymorphism based on type erasure".

3

u/XDracam Aug 26 '21

It's fine, I know a lot of the lingo. I've got a university background and am active on r/programminglanguages. I'd still like to know how to get polymorphism based on type erasure other than by casting pointers...

2

u/ExtraFig6 Aug 27 '21

https://www.youtube.com/watch?v=bIhUE5uUFOA Here's a talk about exactly that

2

u/XDracam Aug 27 '21

Thanks, added to my list of talks!

1

u/[deleted] Aug 26 '21

I don't, I'm relearning, and I find it disheartening to see how quickly some people dive into very complex semi philosophical approaches to seemingly straightforward things a newcomer is looking for hand holding with.

1

u/XDracam Aug 26 '21

Well, if you want to learn something properly, then you gotta learn the language. Applies to everything, including cooking, construction work, crocheting. Because it allows you to discuss things in a few words where you'd otherwise need several complex paragraphs.

But I can try answering any newcomer questions if you have concrete ones ^