I want to know if there is a better way to solve the code duplication problem on template functions like this
struct Z {
void doSomething() { std::cout << "non-const version\n"; }
void doSomething() const { std::cout << "const version\n"; }
};
void tempFunc( Z& z ) {
z.doSomething(); // calls non-const member function
}
void tempFunc( const Z& z ) {
z.doSomething(); // calls const member function
}
int main() {
Z z;
const Z cz;
tempFunc(z);
tempFunc(cz);
return 0;
}
As you can see, both definitions of tempFunc are the same, so in order to avoid code duplication I rewrote that function under C++20 using constraints like this
void tempFunc( std::convertible_to<Z> auto&& z ) {
z.doSomething();
}
Note the use of std::is_convertible_to<> and it works fine, but I suspect that there should be a standard (or if not an easy to write constraint) to use instead of is_convertible_to<> that restricts the argument matching only to const and non-const references.