c++存儲(chǔ)任意類型的函數(shù)對(duì)象和類-創(chuàng)新互聯(lián)

目錄

創(chuàng)新互聯(lián)公司公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元福海做網(wǎng)站,已為上家服務(wù),為福海各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220

任意函數(shù)對(duì)象

備忘

任意類

備忘

總結(jié)


任意函數(shù)對(duì)象
#include#include#include#include#include 

using namespace std;

templateconcept VoidType = is_void::value;

templateconcept NoneVoidType = !is_void::value;

class BaseService
{
public:
	virtual ~BaseService() {}
	string info;
};

//question: NoneVoidType and VoidType can not used at the mean time
templateclass Service : public BaseService
{
	using FuncType = function;
public:
	Service(FuncType func)
		:func_(func)
	{

	}
	RetType Run(ArgTypes&&... args)
	{
		return func_(forward(args)...);
	}
private:
	FuncType func_;
};

//可變參數(shù)無法偏特化
//template//class Service: public BaseService
//{
//	using FuncType = function;
//public:
//	Service(FuncType func)
//		:func_(func)
//	{
//
//	}
//	void Run(ArgTypes... args)
//	{
//		func_(args...);
//	}
//private:
//	FuncType func_;
//};

class ServiceManager
{
	using BaseServicePtr = shared_ptr;

public:
	templatevoid RegisterService(const string& name, const ServiceType& service)
	{
		service_map_.insert(pair(name, BaseServicePtr(new ServiceType(service))));
	};

	templateRetType Excute(const string& name, ArgTypes&&... args)//
	{
		if (service_map_.count(name))
		{
			auto service_ptr = service_map_[name];
			Service* true_service_ptr = dynamic_cast*>(service_ptr.get());
			return true_service_ptr->Run(forward(args)...);
		}
		else
		{
			return RetType();
		}
	}	
private:
	unordered_mapservice_map_;
};

struct StatusCode
{
	int code = 0;
	
};

ostream& operator<<(ostream& os, const StatusCode code)
{
	os<< "status code: "<< code.code<< endl;
	return os;
}

int main()
{
	ServiceManager manager;
	auto add = [](int a, double b, double& c)->double
	{
		c = a + b;
		return a + b;
	};
	auto add1 = [](int a, double b, double& c)->StatusCode
	{
		c = a + b;
		return StatusCode{ 0 };
	};
	Serviceadd_service(add);
	Serviceadd1_service(add1);
	manager.RegisterService>("add", add_service);
	manager.RegisterService>("add1", add1_service);
	double test = 0;
	cout<("add", 2, 1.08,test)<("add1", 2, 2.08, test);
	cout<< test<< endl;

	//any a = 2;
	//any b = 1.08;
	//any c = 0.f;
	//any d = a + b;
	//cout<< manager.Excute("add", a, b, c)<< endl;
	//cout<< any_cast(c)<< endl;

	return 0;
}
備忘

&& 加 forward完美轉(zhuǎn)發(fā);

有返回值和無返回值函數(shù)沒辦法用一個(gè)類實(shí)現(xiàn),通過額外信息記錄。

任意類
class A {
public:
    static A* Create() { return new A(); }
}; 

Instance::Register("A", A::Create);
auto a = Instance::Create("A");
備忘

c++ 無法存儲(chǔ)類型,只能存儲(chǔ)產(chǎn)生該類型對(duì)象的函數(shù);

總結(jié)

c++ 沒有反射系統(tǒng),在rpc等場景中需要構(gòu)建用戶級(jí)反射系統(tǒng),以便于存儲(chǔ)類型或者函數(shù)。

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)頁題目:c++存儲(chǔ)任意類型的函數(shù)對(duì)象和類-創(chuàng)新互聯(lián)
文章來源:http://bm7419.com/article44/hdjhe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄標(biāo)簽優(yōu)化、定制開發(fā)、企業(yè)網(wǎng)站制作、Google、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)