最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

北大同學(xué)黃雨松昨天講北太天元插件開(kāi)發(fā)時(shí)涉及到函數(shù)句柄以及如何傳參數(shù)

2023-05-14 02:04 作者:盧朓  | 我要投稿


#include <cstdlib>

#include <iostream>

#include <typeinfo>

#include <utility>


using namespace std;


namespace ParseParams {

???template <class _T>

??????class FunTrait;

???template <typename R, typename... Args>

??????class FunTrait<R(Args...)>{

?????????public:

????????????static const size_t n_args = sizeof...(Args);

?????????public:

?????????int required_params;

?????????void **passed_args_ptr;

?????????//變量類(lèi)型函數(shù)句柄, 變量名是decorated_func

?????????R(*decorated_func)

????????????(Args...);



?????????public:

?????????FunTrait(R (*func)(Args...), int num_required = 0){

????????????decorated_func = func;

??????????required_params = num_required;

????????????passed_args_ptr = new void *[n_args] {};

????????????for(size_t i=0;i<n_args;i++){

???????????????passed_args_ptr[i] = nullptr;

????????????}

?????????}


?????????template <size_t... I>

?????????R eval_impl(std::index_sequence<I...>){

????????????std::cout<<"需要的參數(shù)個(gè)數(shù)是 = " << required_params << std::endl;

????????????return decorated_func((Args)passed_args_ptr[I]...);

?????????}


?????????R eval(){

????????????return eval_impl(std::make_index_sequence<n_args>());

?????????}


?????????void check_in_args_type(const int * a){

????????????passed_args_ptr[0] = (void *)a;

?????????}


?????????void check_in_args_type(const int * a, const std::string * str){

????????????passed_args_ptr[0] = (void *)a;

????????????passed_args_ptr[1] = (void *)str;

?????????}



??????};

}


double f(){

???std::cout<<" f is a function "<<std::endl;

???return 1.0;

}


double g(const int * j){

???std::cout<<" *j = "<< *j<<std::endl;

???return (double)(*j);

}


char h(const int *j , const std::string?*str ){

???std::cout<<" *j = "<<*j << std::endl;

???std::cout<<" *str = "<<*str << std::endl;

???std::cout<<" (*str)[*j] = "<<(*str)[*j]<< std::endl;

???return (*str)[*j];

}


int main(){

???using namespace ParseParams;

???typedef FunTrait<decltype(f)> FT;

???//typedef FunTrait<double()> FT;


???FT t(f,0);

???t.eval();


???FunTrait<decltype(g)> s(g,1);

???int a = 3;

???s.check_in_args_type(&a);

???s.eval();


???FunTrait<decltype(h)> q(h,2);

???std::string str = "LOVE";

???int i = 1;

???q.check_in_args_type(&i, &str);

???q.eval();


???return 0;

}



北大同學(xué)黃雨松昨天講北太天元插件開(kāi)發(fā)時(shí)涉及到函數(shù)句柄以及如何傳參數(shù)的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
宜春市| 长汀县| 乌兰浩特市| 武胜县| 龙岩市| 新民市| 抚州市| 孟连| 昆明市| 双鸭山市| 宣威市| 和林格尔县| 逊克县| 仲巴县| 洮南市| 佳木斯市| 通道| 金山区| 玛多县| 磐石市| 诸暨市| 敦煌市| 邯郸县| 康乐县| 张家港市| 揭阳市| 定州市| 社会| 民勤县| 历史| 东台市| 紫阳县| 湘乡市| 黄大仙区| 黔西县| 册亨县| 民权县| 南通市| 沅江市| 福海县| 麦盖提县|