I have a problem with duplication of identical code for const and non-const versions. I can illustrate the problem with some code. Here are two s
struct Aggregate
{
int i;
double d;
template
void operator()(Visitor &v)
{
visit(this, v);
}
template
void operator()(Visitor &v) const
{
visit(this, v);
}
private:
template
static void visit(ThisType *self, Visitor &v) {
v(self->i);
v(self->d);
}
};
OK, so there's still some boilerplate, but no duplication of the code that depends on the actual members of the Aggregate. And unlike the const_cast approach advocated by (e.g.) Scott Meyers to avoid duplication in getters, the compiler will ensure the const-correctness of both public functions.