首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可变模板折叠程序在gcc9中失败

可变模板折叠程序在gcc9中失败
EN

Stack Overflow用户
提问于 2019-07-07 15:42:08
回答 1查看 52关注 0票数 0

我在-std=c++14和-std=c++1z中使用g++ 8.3实现了如下程序的正常工作。但是,当我升级到g++ 9.1时,它只能在-std=c++14上工作,而不适用于更高的版本。

代码语言:javascript
复制
#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时生成以下错误消息(与其兄弟姐妹一起):

代码语言:javascript
复制
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>}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56923662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档