Error with variadiac template: “parameter pack must be expanded”

核能气质少年 提交于 2019-12-05 11:31:36

问题


Here's a variadic template function that I've written:

template<class Container, class Value, class... Args>
Value& insert(Container& c, Args&&... args) {
    c.emplace_back(args);
    return c.back();
}

When I use insert like this I'm getting an error:

list<int> lst;
int& num = insert<list<int>, int, int>(lst, 4);

The error complains about this line in the body of insert:

c.emplace_back(args); // <= 'args' : parameter pack must be
                      //             expanded in this context

What does that mean and how can I fix it?


回答1:


The error is due to the missing ellipsis (...) after args when passing all individual parameters (rather than the parameter pack) to emplace_back.

The fixed (and improved) version:

template<class Container, class... Args>
auto insert(Container& c, Args&&... args) -> decltype (c.back()) {
    c.emplace_back(std::forward<Args>(args)...);
    return c.back();
}


来源:https://stackoverflow.com/questions/20588191/error-with-variadiac-template-parameter-pack-must-be-expanded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!