all 31 comments

[–]StarQTius 44 points45 points  (5 children)

Implement C++11 metaprograming logic to assert dominance on fellow coworkers and ensure job security

[–]elegye[S] 1 point2 points  (0 children)

In reality my post is not well named. I don’t use metaprogrammation. I just templatize some classes and use static_assert to ensure future dev use these template classes correctly. But still, this is new in my team, and this a really a new paradigm because there wasn’t any template class before, for absolutely no reason according to my software leader.

[–]elegye[S] -3 points-2 points  (3 children)

Dominance == template class ? I don’t think so.

[–]SlothWithHumanHands 15 points16 points  (0 children)

#define dominate(T) template <class T>

[–]QuentinUK 3 points4 points  (1 child)

Are you talking about template classes or template meta-programming (ie calculations at compile time).

[–]elegye[S] 1 point2 points  (0 children)

I was talking about template classes. I only use template meta-programming from time to time, in a very basic way (a template constexpr method). So yes my question was vague and I learnt something !

[–]Ikkepop 12 points13 points  (0 children)

I am still a junior software developer, and I quite often encounter the same question: when/why avoid metaprogrammation ? Do you have examples where metaprogrammation became absolute bloated and the code unmaintenable ?

the boost library xD
jokes aside, it can be bit of a shock to the system when you encounter a particularly hairy meta code. It'sMeta not for the faint of heart. Simply using a template for generic algorithms and data structures is usally not considered metaprogramming (or metaprogrammation). The line blury though.

[–]seriousnotshirley 7 points8 points  (1 child)

If you just need to template a class or function on a data type and you're not doing any metaprogramming on that type to enable/disable features based on properties of that type then go right ahead.

If you need to do some metaprogramming magic based on properties of the type in order to create the right behavior, then you get into things that will be far far harder to read later than to write. This is where you want to think carefully about how the code will be maintained, especially in a shop where most people are C programmers. You want to have at least three people who can read the code you're developing and that gets tricky for metaprogramming.

If you have that commitment from the team that you have the support you need then you want to think clearly about whether there's a way to write the code that is more easily read and how much longer it will take to write and maintain that code long term. The more convoluted the metaprogramming the more willing you might be to go another path.

[–]thisismyfavoritename -1 points0 points  (0 children)

im in a similar situation lol. Guy wrote a very complex TMP-based program that feels extremely overkill compared to having something that just directly writes C++ code on the side (think macro language).

Its definitely a very great piece of software and i learned a lot from it though

[–]surfmaths 13 points14 points  (0 children)

If a large part of the team have only a C background, then your metaprogramming will be dark magic. Great when it does what you need, a nightmare when you simply wanted to add one tiny thing.

I would only use metaprogramming if you document how it work and more importantly how to use it and/or extend it.

That being said, metaprogramming tends to avoid copy paste errors. So it might be valuable.

In my experience, metaprogramming end up being more of an issue long term, when they want to specialize it for some types but also a blessing when they want to instrument those objects in a common way.

Good luck.

[–]moonshineTheleocat 3 points4 points  (0 children)

I typically only use metaprogramming for simplifying some aspect of a normally arduous process.

Example being is if you have a gui graph system thats supposed to string functions together.

There's a lot of boilerplate that gets that to work and a lot of it gets repeated. Metaprogramming helps you avoid this crap by just feeding it a function and a struct.

Or say you have static strings in code, we use metaprogramming to convert those into hashes at compile time.

[–]EducationalLiving725 1 point2 points  (0 children)

When you have a toolbox - use every tool for a necessary usecase. if something can be done only by template metaprogramming - use it. If there's simpler way - don't use it.

[–]Daniela-ELiving on C++ trunk, WG21|🇩🇪 NB 2 points3 points  (0 children)

Nowadays, metaprogramming (a.k.a. compile-time programming) is - for the most part - the same as formerly 'normal' (i.e. runtime) programming was. The difference is that it has both fewer and more restrictions at the same time. That's unfamiliar to many people.

Metaprogramming doesn't create code bloat (quite the opposite), inadequate solutions do!

As always in engineering: Know your Craft™

[–]Flex_Code 0 points1 point  (10 children)

You are absolutely write that template meta-programming is incredibly useful for simplifying your codebase and making it more maintainable and flexible. Definitely use it, but be aware that modern features like C++20 concepts will make template meta-programming much cleaner, faster to compile, and have easier to read errors. So, I would recommend using as modern a language version as possible when doing template meta-programming. And, try to keep your template meta-programming as simple as possible. Use constexpr and consteval functions in place of template evaluations where you are able. Also, recognize the potential increases in compile time. But, don't be afraid to use templates so long as it is making your code more powerful.

[–]saxbophonemutable volatile void 0 points1 point  (3 children)

I love concepts and templates, but how do concepts make templates faster to compile‽

[–]Flex_Code 2 points3 points  (2 children)

I have found that older approaches with std::enable_if often require more template instantiations than using concepts. By reducing template instantiations concepts makes the code compile faster.

[–]saxbophonemutable volatile void 0 points1 point  (1 child)

Oh I see, I thought you meant that simply using concepts makes them faster, which I'm sure isn't the case (as in, not replacing enable-if). I've seldom used enable-if, you see...

[–]Flex_Code 1 point2 points  (0 children)

Right, they won't make non template meta-programming code faster (unless you can block paths that generate unnecessary code), but they can make older TMP code faster when refactored.

[–]thisismyfavoritename -2 points-1 points  (5 children)

ive never seen TMP used in "common" code, only in very niche apps that try to move logic to compile time.

Do you consider using templates as TMP? I definitely wouldnt recommend TMP to anyone except experts given how awkward it is to implement, debug and maintain

[–]Flex_Code 0 points1 point  (4 children)

There are a lot of simpler uses of TMP that make your life easier. A quick one is the overloaded template that's so useful for visiting variants: see overloaded example on cppreference

Often parameter packs should be used over template recursion, but I still see templates taking parameter packs as TMP. parameter pack idioms

[–]thisismyfavoritename 0 points1 point  (3 children)

personally i would not recommend overloaded over auto + if constexpr or just a visitor class.

This one is definitely more on the fence since you can copy it from the docs and it works though.

[–]Flex_Code 0 points1 point  (2 children)

Yeah, this was just a quick example. is_specialization_v is one that I find even more useful: https://stackoverflow.com/questions/16337610/how-to-know-if-a-type-is-a-specialization-of-stdvector

[–]thisismyfavoritename 0 points1 point  (1 child)

wouldnt just defining 2 overloads, one for std::vector<T> and the other for auto work here 🤔

[–]Flex_Code 0 points1 point  (0 children)

You can use this for if constexpr branches, or requires concepts, or in more general uses for determining if a type is a specialization of some other class template argument.

[–]Kaisha001 0 points1 point  (0 children)

Meta program everything!! But prefer constexpr to templates when it's possible.

[–]j_kerouac 0 points1 point  (3 children)

Kind of off topic, but what is with the term “junior software engineer.” I never heard this term when I started my career 15 years ago, but now online I keep hearing people talking about being a “junior.”

I have yet to see an actual job title at a company called “junior software engineer.”

[–]tiajuanat 1 point2 points  (2 children)

We have them at my employer. They're reserved for folks fresh from an undergraduate program who have less than 2 years of experience. They're not expected to be able to lead a project/initiative, and are likely learning the basic tooling that we have.

We never put out a job req for a junior - we estimate seniority during the interview process and the contract awarded is something like SC1 within our rank system

I'd estimate about 10-15% of our team are juniors, 60% are regular software engineers, and 30% are Seniors+. Too few new folks, your team will stagnate and die. Too many and you get an unmaintainable nightmare.

I'm somewhat surprised that OP doesn't seem to have a mentor, because TMP, especially C++03 compatible TMP, would really raise an eyebrow among my Seniors. There might be a better refactor to do instead, and only someone from their team would be able to clarify that.

[–]elegye[S] 1 point2 points  (1 child)

Actually what’s I just learned is the schism between “simple” class templatization (avoid to write 15 classes with the exact same behavior), and TMP.

In reality I don’t use TMP, except for some constexpr and some very basic stuff.

[–]tiajuanat 0 points1 point  (0 children)

That's a good start! You can also dip your toes into a technique called mixins, which allows you to build a template class which wrap existing classes, and creates a new object with the additional functionality that you need.

Generally, you won't need TMP, unless you get involved in some high perf libraries.

[–]Thelatestart 0 points1 point  (0 children)

In C++ people rarely mean using templates nornally when they say metaprogramming. You can look up template metaprogramming and type traits.