Reputation: 1212
I got the following program working as expected with -std=c++14 and -std=c++1z using g++ 8.3. But, when I upgraded to g++ 9.1, it works only when -std=c++14, but not for higher versions.
#include <iostream>
using namespace std;
#if __cplusplus >= 201500L // C++17 or higher
template <typename... Args>
auto sum(Args&&... args)
{
return (0 + args);// + ... );
}
#elif __cplusplus >= 201402L // C++14
auto sum() { return 0; }
template <typename T>
auto sum(T&& t) { return t; }
template <typename T, typename... Rest>
auto sum(T&& t, Rest&&... r)
{
return t + sum(forward<Rest>(r)...);
}
#else
#error "***You need C++14 or higher to compile this program***"
#endif
int main()
{
cout << sum() << " result of 2 + 3=>" << sum(2, 3) <<
" result of 12+15+18=>" << sum(12, 15, 18) << "\n";
#if __cplusplus >= 201500L // C++17 or higher
cout << "Compiled as C++17 program\n";
#else
cout << "Compiled as C++14 program\n";
#endif
}
g++ 9.1 generates the following error message (with a host of its siblings), when I used -std=c++1z, c++17 or c++2a:
In function 'auto sum(Args&& ...)':
error: parameter packs not expanded with '...':
12 | return (0 + args);// + ... );
| ~~~^~~~~~~
note: 'args'
In function 'int main()':
error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'void')
32 | cout << sum() << " result of 2 + 3=>" << sum(2, 3) <<
| ~~~~ ^~ ~~~~~
| | |
| | void
| std::ostream {aka std::basic_ostream<char>}
Upvotes: 0
Views: 55
Reputation: 96236
(0 + args)
is not a valid fold expression.
You want (0 + ... + args)
.
Also, for me GCC 8.3 (with -std=c++1z
) rejects your code.
Upvotes: 1