可变参数模板错误:“必须扩展参数包"; [英] Error with variadiac template: "parameter pack must be expanded"

查看:42
本文介绍了可变参数模板错误:“必须扩展参数包";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我编写的可变参数模板函数:

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();
}

当我像这样使用 insert 时,我收到一个错误:

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

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

错误在insert的正文中抱怨这一行:

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?

推荐答案

该错误是由于在传递所有个体时 args 之后缺少省略号 (...)emplace_back.

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

固定(和改进)版本:

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

这篇关于可变参数模板错误:“必须扩展参数包";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆