19

C++ 初始化列表展开,这个代码是什么意思?

 4 years ago
source link: https://www.zhihu.com/question/443285720/answer/1719851060
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C++ 初始化列表展开,这个代码是什么意思?

template<typename T, typename... Ts> auto printf3(T value, Ts... args…
11
3,760
登录一下,更多精彩内容等你发现
贡献精彩回答,参与评论互动
C++程序猿, 公众号:高级开发者

在元编程场景下初始化列表是能够保证模板可变参数包从左往右执行的,编译器最终会优化掉初始化列表。不过既然有了折叠表达式直接写成这样就好了。

template<typename ...Args>
void print(Args&&... args) {
    (std::cout << ... << args) << '\n';
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK