In C++23 you can use a templated explicit this
parameter in same_function
in the base class:
struct CommonBase{ // equivalent to: // template <typename T> void same_function(this const T &self) void same_function(this const auto &self) { std::cout << "the exact same function but now "; self.different_function(); }};
Pre-C++23 you can use CRTP:
template <typename T>struct CommonBase{ void same_function() const { std::cout << "the exact same function but now "; static_cast<const T &>(*this).different_function(); }};
(Then inherit like this: struct foo<T*> : CommonBase<foo<T*>>
.)
Or you can avoid specializations in the first place, and just use if constexpr
and requires
(or std::enable_if_t
before C++20) to alter the behavior and to disable certain functions respectively.