Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that C++ visitor pattern template function cannot be overloaded

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.

Share To

Development

  • How to realize css two-column layout based on BFC rules

    This article will explain in detail how to achieve the two-column layout of css based on BFC rules. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report