riv*_*riv -1 c++ c++17 visual-studio-2017
为什么以下代码不编译?
#include <iostream>
template<typename... Args>
void print_all( Args&&... args )
{
std::cout << ... << args;
}
int main()
{
print_all( 1, 2, 3, std::endl );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在使用/ std:c ++ 17的VS2017 15.9.7中遇到以下错误
error C2760: syntax error: unexpected token '...', expected 'expression'
error C2660: 'print_all': function does not take 4 arguments
Run Code Online (Sandbox Code Playgroud)
如果删除std :: endl,第二个错误将消失,但是无论使用哪种运算符,任何使用二进制折叠表达式的操作都会出现第一个错误。根据此页面,我的VS版本应支持折叠运算符。
我必须承认,我不知道VS 15.9.7是否支持折叠表达式。但是,由于std::cout << ... << args;不是fold-expression,因此会出现错误。正确的语法是
(std::cout << ... << args);
Run Code Online (Sandbox Code Playgroud)
还要注意,这std::endl是一个模板函数,因此您不能像以前那样简单地传递它(因此会出现第二个错误)。
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |