我有一个必须使用许多不同类型调用的重载函数。简单的方法是:
"extra_arguments": [
"-vvv",
"--extra-vars",
"'ansible_host={{user `host`}} ansible_python_interpreter=/usr/bin/python3'"
]
如本Q&A所述,可以使用可变参数模板来简洁地表达这些呼叫。代码看起来像这样:
#include <iostream>
#include <type_traits>
#include <utility>
template <bool... Bs>
struct bool_sequence {};
template <bool... Bs>
using bool_and = std::is_same<
bool_sequence<Bs...>,
bool_sequence<(Bs || true)...>
>;
template <bool... Bs>
using bool_or = std::integral_constant<bool, !bool_and<!Bs...>::value>;
int main(int argc, char* argv[])
{
std::cout << bool_or<true>::value << std::endl;
std::cout << bool_or<true, true>::value << std::endl;
std::cout << bool_or<true, true, true>::value << std::endl;
}
但是我必须在代码中的许多地方执行此操作,因此我只想定义一次类型列表。我希望代码在概念上看起来像这样:
uint8_t a;
uint16_t b;
//....
double x;
doSomething(a);
doSomething(b);
//...
doSomething(x);
这怎么办?
答案 0 :(得分:10)
使用一些boiler-plate to obtain a tuple foreach,您可以执行所需的操作,如下所示:
#include <tuple>
#include <utility>
namespace detail
{
template<typename T, typename F, std::size_t... Is>
void for_each(T&& t, F f, std::index_sequence<Is...>)
{
( static_cast<void>(f(std::get<Is>(std::forward<T>(t)))),... );
}
}
template<typename... Ts, typename F>
void for_each_in_tuple(std::tuple<Ts...> const& t, F f)
{
detail::for_each(t, f, std::index_sequence_for<Ts...>{});
}
int main() {
std::tuple<uint8_t, uint16_t, double> tup{};
for_each_in_tuple(tup, [](auto&& arg) {
doSomething(arg);
});
}
答案 1 :(得分:5)
如果您要具有预定义的类型序列,并且不想创建带有参数的元组,则可以使用TypeList方法:
#include<iostream.h>
#include<conio.h>
#include<dirent.h>
#include<dir.h>
#include<process.h>
#include<string.h>
#include<stdio.h>
#include<io.h>
#include<dos.h>
#include<sys/stat.h>
int found=0;
struct ffblk vfile;
unsigned long int udata;
char ch,present[MAXPATH];
int year,month,day;
int next_directory(char *);
void scan_directory(char *);
void main(int account,char *arg[],char *env[])
{
clrscr();
getcwd(present,MAXPATH);
DIR *dir;
struct dirent *temp;
cout<<"\nWelcome to Finder for file view";
cout<<"\n\nEnter drive:";
cin>>ch;
char base[]="X:\\";
base[0]=ch;
if ((dir = opendir(base)) == NULL)
{
clrscr();
cout<<"\nError : Derive not found";
getch();
exit(0);
}
else
{
if(access("FileList.txt",0)==0)
unlink("FileList.txt");
cout<<"\n\n\nScan Date Details:";
cout<<"\n\nEnter Year:";
cin>>year;
cout<<"\nEnter Month:";
cin>>month;
if( !(month>=1 && month<=12) )
{
clrscr();
cout<<"\nError:Value of month is not exist";
getch();
exit(0);
}
cout<<"\nEnter Day:";
cin>>day;
if( !(day>=1 && day<=31) )
{
clrscr();
cout<<"\nError:Value of day is not exist";
getch();
exit(0);
}
}
scan_directory(base);
while ((temp = readdir(dir)) != NULL)
{
char *directory = (char *) malloc(3+strlen(temp->d_name)+1);
strcpy(directory,base);
strcat(directory,temp->d_name);
next_directory(directory);
free(directory);
}
closedir(dir);
clrscr();
if(access("FileList.txt",0)==0)
cout<<"\n\nSystem: Successfully Find all files and stored in FileList.txt";
else
cout<<"\n\nSystem: No file has Modified on"<<day<<"//"<<month<<"//"<<year;
sleep(3);
}
int next_directory(char *path)
{
int count=0;
DIR *dirtemp;
char *hold,*temp;
struct dirent *ptemp;
hold=path;
if ((dirtemp = opendir(path)) != NULL)
scan_directory(path);
else
return 0;
while ((ptemp = readdir(dirtemp)) != NULL)
{
char *directory = (char *) malloc(1+strlen(ptemp->d_name)+1);
directory[0]='\\';
strcpy(directory+1,ptemp->d_name);
if(directory[1]!='\.')
{
count=strlen(hold);
temp = (char *) malloc(strlen(hold)+strlen(directory)+1);
strcpy(temp,hold);
strcat(temp,directory);
free(directory);
if(opendir(temp)!=NULL)
next_directory(temp);
temp[count]='\0';
free(temp+count+1);
hold=temp;
}
else
free(directory);
}
closedir(dirtemp);
return 0;
}
void scan_directory(char *tempo)
{
cout<<"\n"<<tempo;
FILE *stream;
struct ftime ft;
if(present[0]==tempo[0])
chdir(tempo);
else
{
setdisk(tempo[0]-65);
chdir(tempo);
}
udata = findfirst("*.*",&vfile,0x02);
while (!udata)
{
stream = fopen(vfile.ff_name,"r");
if(stream==NULL)
{
fclose(stream);
udata=findnext(&vfile);
continue;
}
else
{
getftime(fileno(stream), &ft);
if(year==ft.ft_year+1980 && month==ft.ft_month && day==ft.ft_day)
{
//cout<<"\n"<<tempo<<" "<<vfile.ff_name<<" Hour:"<<ft.ft_hour<<" Min:"<<ft.ft_min<<" Sec:"<<ft.ft_tsec *2;
if(present[0]==tempo[0])
system("cd\\");
chdir(present);
char string[25];
FILE *fp = fopen("FileList.txt","a+");
if(found==0)
{
fputs("Scan Date: ",fp);
itoa(ft.ft_day, string, 10);
fputs(string,fp);
fputc('\\',fp);
itoa(ft.ft_month, string, 10);
fputs(string,fp);
fputc('\\',fp);
itoa((ft.ft_year+1980), string, 10);
fputs(string,fp);
fputs("\n\n",fp);
found=1;
}
unsigned get;
_dos_getfileattr(vfile.ff_name,&get);
if (get & _A_HIDDEN)
fputs("[Hidden File] ",fp);
get=0;
if(ft.ft_hour<10)
get=1;
itoa(ft.ft_hour, string, 10);
fputs(string,fp);
fputc('\\',fp);
if(ft.ft_min<10)
get=get+1;
itoa(ft.ft_min, string, 10);
fputs(string,fp);
fputc('\\',fp);
if(ft.ft_tsec<10)
get=get+1;
itoa(ft.ft_tsec, string, 10);
fputs(string,fp);
for(int count=0;count<get;count++)
fputs(" ",fp);
fputs(" ",fp);
fputs(tempo,fp);
fputs(" ",fp);
fputs(vfile.ff_name,fp);
fputc('\n',fp);
fclose(fp);
if(present[0]==tempo[0])
chdir(tempo);
else
{
setdisk(tempo[0]-65);
chdir(tempo);
}
}
fclose(stream);
}
udata=findnext(&vfile);
}
if(present[0]==tempo[0])
system("cd\\");
chdir(present);
}
答案 2 :(得分:1)
使用std::tuple
和std::apply
std::tuple<uint8_t, uint16_t, double> tup{};
std::apply([](const auto&... arg) { (doSomething(arg), ...); }, tup);