当前位置:网站首页>可变参表达式
可变参表达式
2022-06-11 13:25:00 【发如雪-ty】
有如下的一个模板函数
template<typename...T>
auto print_result(T... args)
{
(cout << ... << args) << "结束" << endl;
return (... + args);
}
int main()
{
cout << print_result(10, 20, 30, 40) << endl;
system("pause");
return 0;
}
结果:
现在有这样一个需求,希望每个参数的值都扩大原来的2倍,然后再进行彼此间的加法运算。要解决这个问题,比较好的方式是引入一个中间的函数模板,如下:
template<typename...T>
auto print_calc(T... args)
{
return print_result(2 * args ...);
}
上面的范例中2args…这个写法,其实就是可变参表达式,等价于print_result(210,220,230,2*40);
int main()
{
cout << print_result(10, 20, 30, 40) << endl;
cout << print_calc(10, 20, 30, 40) << endl;
system("pause");
return 0;
}
结果:
边栏推荐
- [bug resolution] the form is paged to display the total data res.data total
- 字节真的是宇宙尽头吗?
- tampermonkey这玩意如何替换flash播放器为h5播放器?
- Bs-xx-007 registered residence management system based on JSP
- 使用华为HECS云服务器打造Telegraf+Influxdb+Grafana 监控系统【华为云至简致远】
- create_ Error in engine MySQL connector encryption method
- 微软再曝“丑闻”:在办公室看 VR 黄片,“HoloLens 之父”即将离职!
- How does Cassandra, an open source database giant, tell a "new story" in China? Face to face
- 不谈赛道,不聊风口,开源数据库巨头Cassandra如何在国内讲好“新故事” | C位面对面
- [bug resolution] after uploading the image, cancel the upload and upload again. The last image still exists
猜你喜欢

苹果将造搜索引擎?

On the life extension of distributed locks -- redis based distributed locks

How about NFT market? Why is NFT so popular? How to build NFT platform

Can't understand kotlin source code? Starting with the contracts function~

Why does each running Department display that the database already exists, delete the database, and then succeed? Each running department must delete the database, and re run the whole successfully
![[signal processing] digital signal processing matlab design with GUI interface and report](/img/bd/7bcb03b5619998dda4c07bbf6d6436.png)
[signal processing] digital signal processing matlab design with GUI interface and report

Network information system emergency response

Dbutil auxiliary class, manual commit transaction, metadata

字节真的是宇宙尽头吗?

Bs-xx-007 registered residence management system based on JSP
随机推荐
Is byte really the end of the universe?
SAP Spartacus checkout 流程使用 url 粘贴直接跳转到 delivery mode不能打开页面的原因
不谈赛道,不聊风口,开源数据库巨头Cassandra如何在国内讲好“新故事” | C位面对面
In 2022, capture these 12 data and analyze trends!
Hashicopy之nomad应用编排方案06(配置task)
Checkout design in SAP Spartacus
Hamad application scheduling scheme 06 of hashicopy (configure task)
Musk says he doesn't like being a CEO, but rather wants to do technology and design; Wu Enda's "machine learning" course is about to close its registration | geek headlines
The Tree (AVL, 2-3-, 红黑,Huffman)
InfoQ 极客传媒 15 周年庆征文|移动端开发之动态排行【MUI+Flask+MongoDB】
Add function drop-down multiple selections to display the selected personnel
kubernetes 二进制安装(v1.20.16)(五)验证 master 部署
InfoQ geek media's 15th anniversary essay solicitation - dynamic ranking of mobile terminal development [mui+flask+mongodb]
The tree (AVL, 2-3-, red black, Huffman)
Application choreography nomad vs. kubernetes
漫谈软件缺陷与漏洞
Terraformer importing cloud resources
Kubernetes certificate collection
Quic resistance
kubernetes 二进制安装(v1.20.15)(六)部署WorkNode节点