#include <iostream>
#include <cstdint>
struct CheckValue{
CheckValue(int a,int b,int c){
}
void functions(int a,int b,int c){
std::cout << a << b << c << std::endl;
}
};
class CheckValueV2{
public:
explicit CheckValueV2(const std::string&msg){
std::cout << "Init" << std::endl;
}
int functions(const std::string& msg){
std::cout << msg << std::endl;
return 0;
}
};
//完美转发
template<class T,typename ... Args>
void createT(Args ... args){
T(std::forward<Args>(args)...).functions(std::forward<Args>(args) ...);
}
int main() {
createT<CheckValue>(1,2,3);
createT<CheckValueV2>("hello world");
}
外部接口,使用模板适配
#include <iostream>
#include <cstdint>
#include <vector>
#include <algorithm>
struct CheckValue{
CheckValue(int a,int b,int c){
}
void functions(int a,int b,int c){
std::cout << a << b << c << std::endl;
}
};
class CheckValueV2{
public:
explicit CheckValueV2(const std::string&msg){
std::cout << "Init" << std::endl;
}
int functions(const std::string& msg){
std::cout << msg << std::endl;
return 0;
}
};
//完美转发
template<class T,typename ... Args>
void createT(Args ... args){
T(std::forward<Args>(args)...).functions(std::forward<Args>(args) ...);
}
template<class T>
class Sum{
private:
T sum{0};
public:
void operator() (T b){
sum = sum + b;
}
T get(){
return sum;
}
};
int main() {
createT<CheckValue>(1,2,3);
createT<CheckValueV2>("hello world");
std::vector<int> a{1,2,3,4};
Sum<int> sum = std::for_each(a.begin(),a.end(),Sum<int>());
std::cout << sum.get() << std::endl;
return 0;
}
算法匹配
(优化,必须同一个父类才可以调用)父类指针 -> 子类指针
业务代码和算法代码可以进一步优化
C23 标准 为了更好的在C++ 中兼容,引入了C++中的几个关键字 constexpr nullptr 等
引用他人的截图
标签:std,...,cout,int,args,特性,C++,include,模板 From: https://blog.csdn.net/weixin_45647912/article/details/142756605别守旧 ,用新的 , stl 库做了好多优化,但是别用最新的