2013-05-17 59 views
0

我已经写了下面的模板成员函数,但我不能把它而不被编译器收到错误:模板成员函数的错误:铛不匹配任何成员函数

template <class T, class A> 
auto tpool::enqueue(T&& func, std::vector<A>&& args) 
-> std::vector<std::future<decltype(std::forward<T>(func)(decltype(std::forward<A(args))::value_type))>> 
{ 
    //... 
} 

tpool tp(); 
auto f = [] (int) { /* ... */ }; 
std::vector<int> args; 

tp.enqueue(f, args); 

我得到以下错误通过铛:

test_cpp.cpp:144:5: error: no matching member function for call to 'enqueue' 
    tp.enqueue(f, args); 

test_cpp.cpp:107:13: note: candidate template ignored: substitution failure [with T = <lambda at test_cpp.cpp:140:11> &, A = int]: no matching function for call to 'forward' 
auto tpool::enqueue(T&& func, std::vector<A>&& args) 
+1

通过'tpool tp();'你的意思是'tpool tp;'? – kennytm

+0

你有几个语法错误。例如,'std :: forward stardust

回答

2
template <class T, class A> 
auto tpool::enqueue(T&& func, std::vector<A>&& args) 

这使得args是一个rvalue参考,这仅接受右值,但在

std::vector<int> args; 
tp.enqueue(f, args); 

args是一个左值,因此候选被忽略。

请注意,T&& func允许左值绑定,因为模板替换可以允许T本身是一个左值引用,然后我们有(T&)&& == T&。但是这对于args是不可能的,因为不管A是什么,std::vector<...>&&总是一个向量的右值引用。


如果你不打算复制或修改args反正,你可以传递,而不是一个常量参考:

template <class T, class A> 
auto tpool::enqueue(T&& func, const std::vector<A>& args) 

你也可以让完美转发,不指定该args绝作为一个矢量:

template <class T, class V> 
auto tpool::enqueue(T&& func, V&& args) 
    -> std::vector<std::future<decltype(func(args.front()))>>