whether a template parameter is true?
Use case: I was making a class that has basically 2 version, 1 basic and 1 advanced. The advanced version is just a bolt on some aspects, while reusing most code for the basic version (except one line of code, I decided to guard it with if constexpr. The problem is, I would not like for the advanced functions to even exist unless it's enabled (there would be inconsistencies). I have added static_assert for checking that atm.
See:
template <typename T, bool ADV = false>
class DS{
private:
...
public:
...
void apply(){
static_assert(ADV);
...
}
void update(){
...
if constexpr(ADV) apply();
...
}
}
I would have instead made the adv version a descendent of basic one (in fact, the relationship is that only) but then I get the problem that I would need to make apply virtual (only solution I could see to not have the descendent have it's own copied of base class's functions, if you have any other idea please tell), which (I may be mistaken, but my search seems to support me) that virtual has performance hits, and this needs to be as efficient as possible.
I am not totally sure that understand your requirement but maybe this can help you: https://hastebin.com/mehogurebu.cpp Is that you expected?
You can use partial specialization
Обсуждают сегодня