摘 要: 泛型函数容器的使用可以解耦对象之间的调用关系,有利于实现高内聚、低耦合的软件设计原则。C++ 标准库中并没有这样的容器,用C++旧标准实现也很困难、很低效。C++1x等新标准发布后,出现了一些更好的实现 方式。本文将在已有设计的基础之上,基于C++17新标准,利用if constexpr、fold expression、std::invoke等新技 术,提供一种泛型函数容器的实现方式。测试表明该实现方式简洁高效,解决了重载函数和某些特殊函数的注册调用问 题,可以显著降低耦合性、提高代码复用性。 |
关键词: C++17;泛型;函数容器;高内聚;低耦合 |
中图分类号: TP311.1
文献标识码: A
|
|
Study on the Implementation of Generic Function Container Based on C++17 |
MIN Jun,LUO Hong1,2
|
1.( 1.Library, Yibin University, Yibin 644000, China;2. 2.College of Literature and Journalism, Yibin University, Yibin 644000, China)
|
Abstract: The application of generic function containers can decouple the calling relationships between objects,conducive to the realization of high cohesion and low coupling software design principles.There is no such container in the C++ standard library,and it is very difficult and inefficient to implement with the old C++ standard.The release of new standards,such as C++1x,has brought some better implementation methods.This paper provides a generic function container implementation method based on the existing design and the new C++17 standard,via some new technologies such as if constexpr,fold expression,and std::invoke.Test results show that the simple and efficient implementation method effectively solves the problem of registration and calling of overloaded functions and some special functions,significantly reducing coupling and improving code reusability |
Keywords: C++17;generic;function container;high aggregation;low coupling |