Quantcast
Channel: User HolyBlackCat - Stack Overflow
Viewing all articles
Browse latest Browse all 946

Answer by HolyBlackCat for C++ Define member functions for all template specialized classes

$
0
0

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.


Viewing all articles
Browse latest Browse all 946

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>