繁体   English   中英

C ++ 17折叠表达式未编译

[英]C++17 Fold Expression not Compiling

第一次发布,如果格式不佳,请您谅解。 前几天,我第一次遇到折叠表达式,并且正在尝试使用它们。 但是,我所有的尝试都未能编译。 我将其归结为以下内容:

//test.cpp

template<typename... types>
auto adder(types&... args){
    return (args+...);
}

int main(){return 0;}

我用

g++ -std=c++17 test.cpp

并且它产生以下错误:

testCode.cpp: In function 'auto adder(types& ...)':
testCode.cpp:5:15: error: expected primary-expression before '...' token
  return (args+...);
               ^
testCode.cpp:5:15: error: expected ')' before '...' token
testCode.cpp:5:18: error: parameter packs not expanded with '...':
  return (args+...);
                  ^
testCode.cpp:5:18: note:         'args'

从我所看到的一切来看,这应该是可行的,因此,如果有人可以告诉我我在做什么错,我将不胜感激。

我正在使用xenial linux(在chrome OS上的烤面包片上)和g ++的全新安装。

也许是这样吗?

#include <string>
#include <iostream>

using namespace std::literals;

template<typename... T>
auto add(const T&... args) {
        return (args + ...);
}

int main() {
        std::cout << add(1,2,3,4) << "\n";
        std::cout << add("a"s, "b"s, "c"s) << "\n";
        return 0;
}

正如Kenzel在评论中所怀疑的那样,这是g ++编译器的版本控制问题。 不确定默认情况下我的计算机正在安装什么,但是转到linux系统信息库并手动安装更新版本的g ++解决了该问题。 感谢大家的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM