Программа сворачивания шаблонов Variadic не работает в gcc9 - PullRequest
0 голосов
/ 07 июля 2019

Я получил следующую программу, работающую, как и ожидалось, с -std = c ++ 14 и -std = c ++ 1z с использованием g ++ 8.3. Но когда я обновился до g ++ 9.1, он работает только при -std = c ++ 14, но не для более высоких версий.

#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 генерирует следующее сообщение об ошибке (с хостом своих братьев и сестер), когда я использовал -std = c ++ 1z, c ++ 17 или 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>}

1 Ответ

1 голос
/ 07 июля 2019

(0 + args) не является допустимым выражением сгиба.

Вы хотите (0 + ... + args).


Также, для меня GCC 8.3 (с -std=c++1z) отклоняет ваш код.

...