In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to solve the problem that the C++ visitor mode template function cannot be overloaded". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to solve the problem that C++ visitor mode template function cannot be overloaded"!
background
Recently we encountered a tricky scenario where we had a bunch of modules with a common base class, which we assumed to be BaseClass, with some common structure and overloaded methods. These modules have a heap with the same name but different types.
Parameters, such as:
int DerivedClass1::DoNlpTask(const DerivedReq1& req, DerivedResp* resp);
Something like that. DoNlpTask of each DerivedClass has the same name and different parameters, and these should be implemented for the business. Normally, it's easy to think of ways to overload functions, but unfortunately template functions cannot be overloaded.
However, we want to decouple the framework layer from the business layer, and do not want to contaminate the structure of the framework scheduling module with specific types of services.
solutions
Here, we use the idea of visitor pattern, combined with C++ template to unify the process. First define a Visitor, which defines the general operation steps of specific business modules; during implementation, use template specialization to instantiate different business modules and their DoNlpTask functions. In this way, you only need to call Visitor's unified template interface at the framework level. See the code for details.
final code
#include #include //following simulated request protocols ============================//virtual int get_field_id() = 0; virtual void WormUp () {}};#define REGISTER_PROTOTYPE(req_type, resp_type) \ public: \ using ReqType = req_type; \ using RespType = resp_type;class DerivedClass : public BaseClass { //REGISTER REQUEST TYPE REGISTER_PROTOTYPE(DerivedReq, DerivedResp) public: int DoNlpTask(const DerivedReq&, DerivedResp* resp) { std::cout get_field_id(); std::cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un