模板参数包和函数参数包


template<typename... Args>  //Args是一个模板参数包
void Show(Args... args)     //args是一个函数参数包
{
	int size = sizeof ...(args);
	return;
}

int main()
{
	Show(1, 2, "2");//正确
	Show<>(1, 2, "2");//正确
	Show<int>(1, 2, "2");//正确
	Show<int, int>(1, 2, "2");//正确
	Show<int, int, string>(1, 2, "2"); //正确
	Show<int, int, int>(1, 2, "2"); //错误
	Show<int, int, int, int>(1, 2, "2"); //错误
	return 0;
}

结论是:模板参数包,可以少,但是要和函数参数包对应的上。

#include<iostream>
#include <map>
#include <vector>
#include<algorithm>
#include<tuple>

using namespace std;

/*using family_id = uint32_t;
using Entity = uint64_t;

template<typename Entity, typename... Component>
class XView
{
public:
	template<typename Action>
	void each(Action act) const
	{

	}
};

template<typename...>
class XSparse_Container;

template<typename Entity>
class XSparse_Container<Entity>
{

};

template<typename Entity, typename Component>
class XSparse_Container<Entity, Component> : public XSparse_Container<Entity>
{

};

template<typename C>
struct XWrapper final : XSparse_Container<Entity, C>
{
	//XWrapper(framework* ref) noexcept
};


using underlying_pool_type = XSparse_Container<Entity>;
using pool_ptr = underlying_pool_type*;

struct Pool_Data
{
	family_id _component_id{ 0 };
	pool_ptr _pool{ nullptr };
};



template<typename C>
using Pool_Wrapper = XWrapper<std::decay_t<C>>;


template<typename Entity>
class XFramework
{
public:
	template<typename... Component>
	XView<Entity, Component...> view()
	{
		return { assure<Component>()... };
	}

	template<typename Component>
	auto* assure()
	{
		const family_id type = 3;// get_type<Component>();
		Pool_Data* p = nullptr;
		vector<Pool_Data> _pools;
		if (type >= _pools.size())
		{
			_pools.reserve(type << 1);

			while (type >= _pools.size())
			{
				_pools.emplace_back();
			}
		}

		p = &_pools[type];

		if (nullptr == p->_pool)
		{
			p->_component_id = type;
			p->_pool = new Pool_Wrapper<Component>();
		}
		return static_cast<Pool_Wrapper<Component>*>(p->_pool);
	}
};


template<typename... Component>
XView<int, Component...> view()
{
	return {};
};*/

void P(int a)
{
	cout << a << endl;
}

inline void args_print(std::ostream& steam) {}

template<typename T, typename ...Args>
inline void args_print(ostream& steam, T first, Args...rest) {
	steam << first;
	//steam << typeid(first).name << endl;
	args_print(steam, rest...);//剩余参数递归调用
}

template<typename T>
void TT(T a)
{
	std::cout << a << endl;
}


template<typename Entity, typename... Component>
class XView
{
public:
	XView(Component ... args)
	{
		size = sizeof ...(args);
		args_print(std::cout, args...);
	}

	template<typename Action>
	void each(Action act) const
	{
		auto result = true;
		
		//int size = sizeof ...();
		//for (int i = 0; i < size; ++i)
		((result ? each<Component>(std::move(act)) : void()), ...);

		//(TT<Component>(act), ...);
	}

	template<typename C, typename Action>
	void each(Action act) const
	{
		C c = 1;
		int a = 1;
	}
	int size;
};


template<typename ComponentA, typename ComponentB>
int f()
{
	return 1;
}

template<typename Entity, typename... Component>
XView<Entity, Component...> view()
{
	//auto a = XView<Entity, Component...>(args...);
	//return a;
	//return { f<Component...>(),f<Component...>() };
	return { f<Component...>(), f<Component...>() };
	//return { 3,4 };
}

class A
{
public:
	A(int a, int b)
	{

	}
};

A f()
{
	return { 1,2 };
}



template<typename T, typename ...B>
void func(B ... args)
{
	int size = sizeof ...(args);
	T a;
	//B b;
	//int size = sizeof ...(args);
	//args_print(std::cout, args...);
}

#include <tuple>

template<typename ... T>
void SY()
{
	std::tuple<T...> a;
}



class base
{
public:
	int a;
};
class p1 : public base
{
public:
	p1() { a = 1; }
};

class p2 : public base
{
public:
	p2() { a = 2; }
};

void SY2(base b)
{
	std::cout << b.a << std::endl;
}

template<typename ...T>
void SY1()
{
	std::tuple<T...> a;

	(SY2(std::get<T>(a)), ...);
}


template<typename T1, typename T2>
void addx(T1 t1, T2 t2)
{
	//auto flag = is_same_v<T1, string>;
	typedef conditional_t<is_same_v<T1, string>, int, string> TT;
	TT t = "abc";
}

template<typename... Type>
struct type_list
{
	static constexpr auto size = sizeof...(Type);
public:
	type_list()
	{
		int a = 1;
		int b = 2;
	}
};

template<typename...>
struct type_list_cat {
public:
	type_list_cat()
	{
		int a = 1;
	}
};

template<typename... Type>
struct type_list_cat<type_list<Type...>>
{
public:
	using type = type_list<Type...>;
	int a = 1;
public:
	type_list_cat(int x)
	{
		auto size1 = sizeof...(Type);
		int a = 1;
		int b = 2;
	}
};

template<typename... Type, typename... Other, typename... List>
struct type_list_cat<type_list<Type...>, type_list<Other...>, List...>
{
	using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
public:
	type_list_cat()
	{
		auto size1 = sizeof...(Type);
		auto size2 = sizeof...(Other);
		auto size3 = sizeof...(List);

		int a = 1;
		int b = 2;
	}
};

template<typename ...>
class Person;

template<typename T>
class Person<T>
{
public:
	Person(int x)
	{
		int a = 3;
		int b = 3;
	}
};

template<typename ...T>
class Student
{
public:
	Student(T... args)
	{
		auto size1 = sizeof...(T);
	}
};


int main()
{
	/*Person<int> axx(3);

	type_list<int, int> d;*/
	
	type_list_cat<int, int> xa(type_list<int, int>);

	type_list_cat<int, int> test0;
	type_list_cat<type_list<int, int>> test1(1);
	type_list_cat<type_list<int, int, float>> test2(2);
	type_list_cat<type_list<int>, type_list<int, int>, type_list<int, int, int>> test3;
	type_list_cat<type_list<int>, type_list<int, int>, type_list<int, int, int>>::type test4;
	int xxx = test4.size;

	std::cout << typeid(test1).name() << std::endl;
	std::cout << typeid(test2).name() << std::endl;
	
	type_list_cat<type_list<int, int, float>>::type x;

	//std::cout << typeid(xa).name() << std::endl;
	func<string, string>("123", "456");

	//addx<int>(1, 2);


	//XView<string, int, int> xview(1, 1);
	//float c = 10;
	//xview.each(c);

	std::cout << "t1 is {" << std::get<0>(t1) << ", " << std::get<1>(t1) << "}" << std::endl;

	//std::tuple<int, string, string> t(1, "abc", "def");
	//auto x = std::get<int>(t);
	//auto y = std::get<1>(t); //必须传递索引才行,因为有两个string
	//auto z = std::get<2>(t);

	//SY1<p1, p2>();

	return 0;
}

vector的size和capacity

	vector<int> v;
	int size = v.size();
	int capacity = v.capacity();
	v.reserve(5);
	
	v.emplace_back(1);
	v.emplace_back(2);
	v.emplace_back(3);

	size = v.size();
	int x = v.capacity();
template<typename...>
class XSparse_Container;

template<typename Entity>
class XSparse_Container<Entity>
{
public:
	XSparse_Container() = default;

public:
	int size() const {
		return length;
	}

	int length;
};

template<typename Entity, typename Component>
class XSparse_Container<Entity, Component> : public XSparse_Container<Entity>
{

};

using Entity = uint64_t;
using underlying_pool_type = XSparse_Container<Entity>;
template<typename C>
using pool_type = XSparse_Container<Entity, C>;

template<typename Entity, typename... Component>
class XView
{
public:
	tuple<pool_type<Component>*...> _pools;
	XView(XSparse_Container<Entity, Component>*... ref) noexcept
		:_pools{ ref... }
	{
		get_candidate();
	}
	underlying_pool_type* _candidate;

	void get_candidate() noexcept
	{
		auto a = { static_cast<underlying_pool_type*>(std::get<pool_type<Component>*>(_pools))... };
		cout << typeid(a).name() << endl;


		_candidate = std::min(a, [](const auto* lhs, const auto* rhs)
			{
				 cout << typeid(lhs).name() << endl;
				 //return true;
				return lhs->size() < rhs->size();
			});

		cout << _candidate->length << endl;
	}
};

int main()
{
	XSparse_Container<uint64_t, string> b;
	XSparse_Container<uint64_t, int> c;

	b.length = 2;
	c.length = 3;

	XView<uint64_t, string, int> x(&b,&c);

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值