【c++11】cpp实现模板函数的声明与定义分离

文章目录

一、分离模板的声明与定义方法一

在模板定义的cpp文件中声明其特化版本

other.h

cpp 复制代码
#pragma once

#include <iostream>
#include <string>

using namespace std;

template<typename T>
void show(T t);

oher.cpp

cpp 复制代码
#include "other.h"
template<typename T>
void show(T t)
{
	cout<<"t"<<'\n';
}


template void show<int>(int);
template void show<double>(double);
template void show<string>(string);

二、分离模板的声明与定义方法二

使用variant(C++17语法)代替上述的方式

other.h

cpp 复制代码
#pragma once
#include <iostream>
#include <string>
#include <variant>

using namespace std;

void show(variant<int , double, string> t);

other.cpp

cpp 复制代码
#include "other.h"

void show(variant<int , double, string> t)
{
	cout << t.index() <<endl;
/*
visit内部的实现方式可能就是通过index()选择的;
auto visitor = [](auto&& arg){cout<<arg<<endl;};


if (t.index() == 0)
{
	visitor(get<int>(t));
}
else if (t.index() == 1)
{
	visitor(get<double>(t));
}
else if(t.index() == 2)
{
	visitor(get<string>(t));
}

or
if (holds_alternative<int>(t))
{
	visitor(get<int>(t));
}
else if (holds_alternative<double>(t))
{
	visitor(get<double>(t));
}
else if(holds_alternative<string>(t))
{
	visitor(get<string>(t));
}
*/


	visit([](auto&& arg){cout<<args<<endl;},t);
}

解决了分离模板的声明与实现后,如果想要把每个variant对象保存至vector,出现的问题是:每个variant=32+8,string bytes+index bytes。每次占用内存过大,需要优化。所以使用data-oriented design进行改造

other.h

cpp 复制代码
#pragma once

#include <string>
#include <variant>

using namespace std;

using Object = variant<int, double, string>;

void add_object(Object o);
void print_objects();

other.cpp

cpp 复制代码
#include "other.h"
#Include <vector>

static vector<variant<int.double.string>> objects;
static vector<int> int_objects;
static vector<double> double_objects;
static vector<string> string_objects;

static constexpr int v = sizeof(variant<int.double.string>);

void add_object(variant<int.double.string> o)
{
	objects.push_back(o);objects
}


void print_objects()
{
	for(auto&& o : objects)
	{
		visit([](auto&& arg){std::cout<<arg<<std::endl;},o);
	}
}

other.cpp

cpp 复制代码
#include "other.h"
#Include <vector>

static vector<int> int_objects;
static vector<double> double_objects;
static vector<string> string_objects;


void add_object(variant<int,double,string> o)
{
	if(holds_alternatibe<int>(o))
	{
		int_objects.push_back(get<int>(o));
	}
	else if(holds_alternatibe<double>(o))
	{
		double_objects.push_back(get<double>(o));
	}
	else if(holds_alternatibe<string>(o))
	{
		string_objects.push_back(get<string>(o));
	}
}

void print_objects()
{
	for(auto&& o: int_objects)
	{
		cout<<o<<endl;
	}
	for(auto&& o: double_objects)
	{
		cout<<o<<endl;
	}
	for(auto&& o: string_objects)
	{
		cout<<o<<endl;
	}
}

使用编译器for循环以及tuple简化代码

cpp 复制代码
static tuple<vector<int>. vector<double>, vector<string>> objects;

void add_object(variant<int, double, string> o)
{
	if (o.index() == 0)
	{
		std::get<0>(objects).push_back(std::get<0>(o));
	}

	else if (o.index() == 1)
	{
		std::get<1>(objects).push_back(std::get<1>(o));
	}

	else 	if (o.index() == 2)
	{
		std::get<2>(objects).push_back(std::get<2>(o));
	}
}


void print_objects()
{
	for(auto&& o: std::get<0>(objects))
	{
		cout<<o<<endl;
	}
	for(auto&& o: std::get<1>(objects))
	{
		cout<<o<<endl;
	}
	for(auto&& o: std::get<2>(objects))
	{
		cout<<o<<endl;
	}
}

other.cpp

cpp 复制代码
static tuple<vector<int>. vector<double>, vector<string>> objects;

template<int N>
struct int_constant{
	static constexpr int value = N;
};

template <int N, typename Lambda>
void static_for(Lambda&& lambda)
{
	if constexpr (N > 0)
	{
		static_for<N-1>(lambda);
		int_constant<N-1> ic;
		lambda(ic);
	}
}

void add_objects(variant<int, double, string> o)
{
	static_for<3>([&](auto ic){
		std::get<ic.value>(objects).push_vack(std::get<ic.value>(o));
	});
}

void print_objects()
{
	static_for<3>([&](auto ic){
		for(auto&& o: std::get<ic.value>(objects))
		{
			cout<<o<<endl;
		}
	});
}

取出variant中的所有类型,以vector<x>,vector<y>的形式放在tuple中的类型中。即Object类型->Objects类型

other.cpp

cpp 复制代码
template <class V>
struct variant_to_tuple_of_vector
{
}

template <class ...Ts>
struct variant_to_tuple_of_vector<variant<Ts...>> {
    using type = tuple<vector<Ts>...>;
};

static variant_to_tuple_of_vector<Object>::type objects;

自动提取variant中所有参数的个数,优化lambda只在输入的variant变量中插入

other.cpp

cpp 复制代码
void add_objects(variant<int, double, string> o)
{
	static_for<std::variant_size_v<decltype(o)>>([&](auto ic){
		if (o.index() == ic.value)
		{
			std::get<ic.value>(objects).push_vack(std::get<ic.value>(o));
		}
		
	});
}


void print_objects()
{
	static_for<std::variant_size_v<decltype(o)>([&](auto ic){
		for(auto&& o: std::get<ic.value>(objects))
		{
			cout<<o<<endl;
		}
	});
}

参考

相关推荐
Trouvaille ~27 分钟前
【C++篇】C++类与对象深度解析(六):全面剖析拷贝省略、RVO、NRVO优化策略
c++·c++20·编译原理·编译器·类和对象·rvo·nrvo
little redcap28 分钟前
第十九次CCF计算机软件能力认证-乔乔和牛牛逛超市
数据结构·c++·算法
AI原吾1 小时前
掌握Python-uinput:打造你的输入设备控制大师
开发语言·python·apython-uinput
机器视觉知识推荐、就业指导1 小时前
Qt/C++事件过滤器与控件响应重写的使用、场景的不同
开发语言·数据库·c++·qt
毕设木哥1 小时前
25届计算机专业毕设选题推荐-基于python的二手电子设备交易平台【源码+文档+讲解】
开发语言·python·计算机·django·毕业设计·课程设计·毕设
珞瑜·1 小时前
Matlab R2024B软件安装教程
开发语言·matlab
weixin_455446171 小时前
Python学习的主要知识框架
开发语言·python·学习
muyierfly1 小时前
34.贪心算法1
算法·贪心算法
孤寂大仙v1 小时前
【C++】STL----list常见用法
开发语言·c++·list
她似晚风般温柔7892 小时前
Uniapp + Vue3 + Vite +Uview + Pinia 分商家实现购物车功能(最新附源码保姆级)
开发语言·javascript·uni-app