I have some class which looks like this:
struct A {
void *stuff;
int x;
int foo() const;
}
And I have some functions which take parameters of this type, e.g.
int bar(A a1, A2 a2);
int baz(A2 a2);
The thing is, not all of these functions actually change any memory in a2.stuff; yet - I can't take a const void* and an int, construct an A and pass it to such functions. Or rather, I can, but only using const_cast<> which is really not the way to live your life. Plus, I could easily get confused and pass my const_cast<>'ed A to a function which actually modifies data through A::stuff.
So, I decided I want a "constant A". Not an A whose fields are immutable - an A through which you don't change what stuff points to.
If A was somehow templated, i.e. if it the short was some kind of T - then no problem, you replace A<void> with A<const void> and Bjarne's your uncle. But... A isn't templated. And I don't want to make it templated, since I don't want A<int>'s or anything like that to exist.
So, what should I do?
The naive approach is to replicate the definition of A, almost, for a const_A:
struct const_A {
void const *stuff;
int x;
int foo() const;
}
struct A {
void *stuff;
int x;
int foo() const;
operator const_A() const { return const_A { stuff, x }; }
}
but that's repetitive, and if I have 20 methods it's even more annoying to replicate.
Bonus points for a solution without a lot of boilerplate, private members, implementation of all ctors etc.