【发布时间】:2013-08-25 04:56:30
【问题描述】:
我想通过第三方函数调用另一个方法;但两者都使用可变参数模板。例如:
void third_party(int n, std::function<void(int)> f)
{
f(n);
}
struct foo
{
template <typename... Args>
void invoke(int n, Args&&... args)
{
auto bound = std::bind(&foo::invoke_impl<Args...>, this,
std::placeholders::_1, std::forward<Args>(args)...);
third_party(n, bound);
}
template <typename... Args>
void invoke_impl(int, Args&&...)
{
}
};
foo f;
f.invoke(1, 2);
问题是,我得到一个编译错误:
/usr/include/c++/4.7/functional:1206:35: error: cannot bind ‘int’ lvalue to ‘int&&’
我尝试使用 lambda,但 maybe GCC 4.8 尚未处理语法;这是我尝试过的:
auto bound = [this, &args...] (int k) { invoke_impl(k, std::foward<Args>(args)...); };
我收到以下错误:
error: expected ‘,’ before ‘...’ token
error: expected identifier before ‘...’ token
error: parameter packs not expanded with ‘...’:
note: ‘args’
据我了解,编译器希望用int&& 类型实例化invoke_impl,而我认为在这种情况下使用&& 会保留实际参数类型。
我做错了什么?谢谢,
【问题讨论】:
-
GCC 4.8 完美地处理了语法。你尝试了什么?
-
@ArneMertz 我用我尝试过的语法更新了问题
-
看来你刚刚在 gcc 中遇到了一个错误,它应该可以工作:gcc.gnu.org/bugzilla/show_bug.cgi?id=41934
标签: c++ templates c++11 variadic-templates perfect-forwarding