Consider a class Bar
in two versions of a library:
/// v1
class Bar
{
void get_drink()
{
std::cout << "non-const get_drink() called" << std::endl;
}
};
/// v2
class Bar
{
void get_drink()
{
std::cout << "non-const get_drink() called" << std::endl;
}
void get_drink() const
{
std::cout << "const get_drink() called" << std::endl;
}
};
On the client side code, we own a Bar
object and would like to get_drink
. I want to be able to prefer calling the const version of get_drink()
if it's available (when using v2 library) and fallback to the non-const version (when using v1 library). That is:
Bar bar;
bar.get_drink(); // this does not call the const version of v2
static_cast<const Bar&>(bar).get_drink(); // this does not compile against v1 library
Unfortunately, the library is not versioned and there's no other way to distinguish between the two versions.
I reckon some template magic has to be used. So the question is how?