5 ms·
I like to code in cpp as a hobby (computer vision stuff) but avoid template programing like the plague. The part of std is already scary stuff with 50 lines of
by papichulo2023 2y ago
I like to code in cpp as a hobby (computer vision stuff) but avoid template programing like the plague. The part of std is already scary stuff with 50 lines of errors for single typo.
- edflsafoiewq 2y agoIt's actually becoming easier as they make it a real programming language. The infamous template error messages are generally a result of two things: first, too many candidate functions because logic is cobbled together out of a nest of SFINAE overloads, which is ameliorated by moving the logic into normal code with if constexpr, etc; and second, type errors occurring deep in the call/expansion stack because unchecked values keep getting passed on just like in a dynamically typed language (which TMP basically is), which is ameliorated by moving the type checks higher up in the stack with concepts or static_assert.
- mb7733 2y ago> 50 lines of errors for single typo 50? That's it!? I work in a code base where a previous developer went nuts with overloads and I routinely get hundreds of lines of output for a single compiler error.
- npalli 2y ago>The part of std is already scary stuff with 50 lines of errors for single typo. IDE's make this less of a problem.
- dkersten 2y agoI personally quite like templates and constexpr. I don’t do anything too crazy with them, but I like being able to write code that will compile down to efficient code without runtime checks. I tried to write equally generic code in typescript recently and I had to make peace with the fact that I had to pay for either an indirect call through inheritance or a runtime conditional, where in C++ I could use template code and a constexpr if to choose the correct code path at compile time.
- rerdavies 2y agoI code cpp professionally. But I still dread template programming. Pretty much a guarantee that I'm going to end up spending an afternoon wading through horrifyingly inscrutable error messages.
- pjmlp 2y agoAfter C++17, template programming has become relatively easy. You can use enable_if and static_assert, alongside type traits to check the types, and give useful error messages. With C++20 you get concepts lite, which while not being as good as C++0x concepts, still simplify the code a lot, improve error messages, and remove the need for the classical tricks, when used alongside constexpr. No need to go crazy with them.
- paulddraper 2y agoSave often, compile often
- dgfitz 2y agoI also refuse to touch templates. Never needed them, never liked them. When I work on code that utilizes templates in c++, it reaffirms this feeling.
- otabdeveloper4 2y agoI hope programming isn't your day job.
- GuB-42 2y agoBut how are you writing generic code then? C preprocessor macros? Abstract classes? No genericity at all? C preprocessor macros are a hack, they are useful, but they come with plenty of issues. If you are using macros, there is a good chance there is a superior solution that uses templates. Abstract classes impact performance, sometimes significantly, and it doesn't solve all problems templates solve. If you don't care, then you would probably be better off doing Java (or languages in the same family) than C++. If you are not using genericity at all, it may actually be a good thing! Too much abstraction is a common problem. But sometimes you need it. Refusing to use generics will limit what you can do. And if it is your job, it will probably limit your pay too.
- mianos 2y ago50 lines? For std io I get 100s of lines of candidate suggestions. It's so dumb. I usually turn off the suggestions error. Only about 35 years as a C++ developer so maybe I will become less grumpy about template errors as I age.