How do you write a generic bitfield-setter in C++?
specifically I noticed I frequently copy-and-paste the same code, so I made an idealistic function, one I’d wish c++ could implement according to circumstances:
template<bool& B> bool s::BoolClamp(bool a)
{
if(a==B)
return false;
B=a;
return true;
}
then I have the bit-field around this function:
struct s{
bool a:1;
bool b:1;
BoolClamp(bool a);
};
finally the test:
int main(){
s t;
t.BoolClamp<t.b>(true);
}
As expected, even if it weren’t about a bit-field, clang says
no matching function for call to ‘BoolClamp’
…
invalid explicitly-specified argument for template parameter ‘B’
not saying the compiler is wrong here, what I intended to say is take this reference to b inside of the class (at its bitfield-position) and use it for the template-argument taking a reference. and the standard obviously does not support that, even less does it su7pport taking bitfields into account.
If it were a full-sized int-member I could pass pointer-to-member of it to the template, and it would work, alebeit without the syntactic sugar of references:
struct s{
template<bool s::*B> bool BoolClamp(bool a)
{
if(a==(*this).*B)
return false;
(*this).*B=a;
return true;
}
bool a:1;
bool b;
};
int main(){
s t;
t.BoolClamp<&s::b>(true);
}
This would give
address of bit-field requested
if b was still in the bit-field, but it works when it isn’t. Hence my idea for a solution using a reference as they by definition are just syntactic sugar.
So it seems the only solution here is to define a text-macro. Any other suggestions for handling bit-fields? Maybe I’m doing this completely wrong? What can I do to create a setter-function that tells me if it changed anything, while having the code short and easy to understand?
My trick with pointer-to-class-member is not easy to understand and requires repeating the class-name when used! Not to mention it does not handle bit-fields. The context is GUI programming; the return value of the setter should notify the caller that a change has occurred and the display should be updated. Originally I repeated the text in BoolClamp for each boolean variable twice, for true-setter and for false-setter. I want to improve readability of code and refactor it into something more easy to read or write in future. Hence the use of templates so it can be used in other classes too. Ubiquity of language requires I give names to code-snippets, so even repeating 24 setters for 12 boolean variables is OK if the body of the setters is just a template function with a good name. any ideas how to program that?
21