|
I never really saw the point, and I feel like template programming nailed fringe conditions. Then again, I don't work in a strong C++ shop. Lots of C developers that think they know C++.
As one other poster said, it just made it that much more difficult to support. Honey you live in the embedded world. Are you actually able to leverage templates to make your life easier? Does it allow you to target a new platform faster?
Charlie Gilley
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
Has never been more appropriate.
|
|
|
|
|
I agree. But C developers can get hung up on the use of macros (sort of like templates, I guess, since I don't use C++).
"A little time, a little trouble, your better day"
Badfinger
|
|
|
|
|
Probably you lean on macros a lot.
I prefer templates when I can use them for a few reasons.
Type checking
Greater flexibility in terms of
Recursively generating multiple code statements from one instantiation
Computing conditions at compile time so you don't have to do them at runtime.
And you can use them in places where you can't really use macros, but the reverse is also true.
To err is human. Fortune favors the monsters.
|
|
|
|
|
good points.
"A little time, a little trouble, your better day"
Badfinger
|
|
|
|
|
Macros are okay, complex macros are a disaster.
Forgive me if I am wrong, but I have always considered macros as a source code substitute. If the macro is made to help simplify code - okay. If you are hiding complex calculations - not so much okay. Put the sucker in a function.
Any time I've run into creative macro code, its an elephant ****.
So, I say again - how do templates make my life easier?
We have the 4 pillars of OO development, blah blah - roll with me here a bit. I support code that has a few layers of inheritance. It's cute but can be painful. It's just not the silver bullet. I love abstraction, but in the end I have to live with this code - I need it to be understandable, not cute.
So back to templates. Templates seem cute to me. They make the code complex to understand and to debug.
Am I wrong? I honestly ask this question. I've read the description of the problems templates are supposed to solve, but 0..0000001% applies to me.
That's why I would love to see you, honey, maybe generate a practical article on how a template solution solved an issue.
Charlie Gilley
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
Has never been more appropriate.
|
|
|
|
|
I've written articles like that Metaprogramming in C++: A Gentle Introduction[^]
This for example, was basically gleaned from the starting point of my GFX library.
Now, as for a real world problem, consider supporting arbitrary binary footprints and color spaces for pixels. Pixels you can declare in any color model, with any bit depth, divided however you want among the color channels.
Now imagine computing the bitshifts to draw to a bitmap with say, RGB666 format, or indexed color, or RGB565, etc.
You can do it at runtime, and you'll get 3 frames per second.
Or you can do it at compile time and get better than 30.
Here's the documentation on my pixel<> template.
This would be impossible to do realistically without templates.
https://honeythecodewitch.com/gfx/wiki/pixels.md[^]
To err is human. Fortune favors the monsters.
|
|
|
|
|
ahhh, off to read.
I fear I must confess that most of the time I live in the world of "add more power". You allude to the nugget that template programming allows you to optimize the performance.
Thank you, I shall read. My initial response would be - why would not polymorphism solve this issue? A detectable change in the interface would transition off to another chunk of code. Why could I not have the data in a generic format and allow my code to adapt?
Note: don't go ballistic on me. I've only struggled with templates (meaning I really don't understand them). The above questions are rhetorical until I read your articles.
Charlie Gilley
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
Has never been more appropriate.
|
|
|
|
|
You can't afford to make one function call per pixel when drawing graphics. That's the only way binary interfaces can really work in this case. If I make a base pixel class and it needs to be squishy enough to accommodate a 24-bit HSV pixel and a 16-bit RGB pixel then every time I act on that pixel a couple of things happen
A) I need to call a function to get any actual usable color information off the thing, or to convert it to a different format.
B) In order to realistically make it "polymorphic" practically, the actual channel values would *have to be* scaled floats.
Now, one function call is not a big deal. Except multiply that times 320x240x30 times per second, on a 240MHz (or worse machine)
It's worse than that. Honestly, polymorphism costs so much comparatively because you can't even do batched pixel operations, like say, filtering multiple pixels at once because the data is no longer available, but rather it's hidden behind a generic binary interface.
Even on a PC I'd expect 10x improvement in performance using templates, which allow you to work with the pixels as a contiguous stream of data that they can act on natively rather than having to translate every element through a conversion interface that genericizes it.
I'm trying to explain it as best I can without actually getting into code. I hope that helps.
To err is human. Fortune favors the monsters.
|
|
|
|
|
In particular see 1.7 in the document i linked to - declaring a custom pixel format:
using index7 = gfx::pixel<gfx::channel_traits<gfx::channel_name::index,4,0,6>>;
using rgb565 = pixel<channel_traits<channel_name::R,5>,
channel_traits<channel_name::G,6>,
channel_traits<channel_name::B,5>>;
Imagine doing that without templates. You'd need a growable vector just to store the channels. Then imagine doing computations on them, having to go through a vector when you should be able to get what you need with a simple shift or two. Actually, scratch that. You'd need a map<> because you can look up channels by name or index.
To err is human. Fortune favors the monsters.
|
|
|
|
|
how did your avatars react ?
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
After avoiding it for about three years I got the virus three weeks ago... Beside some severe muscle pain, and hard time to sleep (probably connected) I had nothing special at the beginning...
But on the 4th day I realized that I do not taste the food and do not smell it...
Even today - three weeks late - I can't cook a decent meal or enjoy it...
I'm devastated...
"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid." ― Albert Einstein
|
|
|
|
|
My wife had a similar problem. It took a month or so, but her sense of taste and smell came back. I understand it varies from person to person. Hope yours doesn't take too much longer to come back.
|
|
|
|
|
Me too...
"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid." ― Albert Einstein
|
|
|
|
|
My sympathy! I got vaccinated in January/February 2021, but that June I think I got a very mild case. I was up for the whole night with serious trouble breathing that was very much worse lying down. Eventually I fell asleep in my armchair, and in the morning I was fine - but, at first, food tasted a bit strange, and then it tasted of nothing. Now, a year and a half later, my sense of taste has just about returned.
One thing I did notice is that cold foods, like salads and fruit, were much less affected than hot, cooked foods.
|
|
|
|
|
As you know, we had it in May 2020.
My hair is still regrowing (and a mess, because it's not long enough to fit in the pony yet but I can't cut it as it just postpones the problem).
And ... my sense of taste is still different to before the bug - even today, there are flavours I can't detect anymore. It never went completely, but it definitely changed, hopefully not permanently. Ithink it's slowing coming back, but it's really difficult to tell!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
That's a long time...
As now I can't tell if something is salty or spicy or sweet... I'm not sure I can stand it for two years...
"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid." ― Albert Einstein
|
|
|
|
|
Well... it should help with dieting though!
|
|
|
|
|
If I just knew... I went on a diet since February, loosing 20 kg...
"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid." ― Albert Einstein
|
|
|
|
|
Quote: loosing 20 kg...
Well done. I reckon losing weight is harder than giving up smoking. Since 2013 I lost 26kg and put 24 back on.
// TODO: Insert something here Top ten reasons why I'm lazy
1.
|
|
|
|
|
Don't know about the long run...
I've actually used an application to create a kind of commitment - also got educated about food and habits...
"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid." ― Albert Einstein
|
|
|
|
|
|
Feel well.
I had a mild case of it this summer, but luckily did not get any of the "can't taste" side effects.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
|
And the anti-vaxers continue to rag on.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Not sure I understand you...
"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid." ― Albert Einstein
|
|
|
|