C++ 如何使用可变模板参数进行模板函数调用?

C++ 如何使用可变模板参数进行模板函数调用?,c++,c++11,templates,c++14,variadic-templates,C++,C++11,Templates,C++14,Variadic Templates,下面是我简单的可变模板函数。此模板将std::tuple作为其输入参数之一。但它拒绝编译,错误为模板参数推断/替换失败 谁能指出我犯的错误吗 #include <tuple> using namespace std; template<typename... TT, typename ReturnType> ReturnType& getValue(int ind, std::tuple<TT...>& t, ReturnType&

下面是我简单的可变模板函数。此模板将std::tuple作为其输入参数之一。但它拒绝编译,错误为模板参数推断/替换失败

谁能指出我犯的错误吗

#include <tuple>

using namespace std;

template<typename... TT, typename ReturnType>
ReturnType& getValue(int ind, std::tuple<TT...>& t, ReturnType& val) {                                                                                                                                                                                                                                                                                                                        
    return val;                                                                                                                                                                                                                                                                                                                                                                               
}

int main() {                                                                                                                                                                                                                                                                                                                                                                                  
    std::string str("Hello"), result;                                                                                                                                                                                                                                                                                                                                                         
    std::tuple<std::string> t = std::make_tuple(str);                                                                                                                                                                                                                                                                                                                                         

    getValue<std::tuple<std::string>, std::string>(0, t, result);                                                                                                                                                                                                                                                                                                                             
    return 0;                                                                                                                                                                                                                                                                                                                                                                                 
}
下面是编译输出

g++ -c tuple.cc -std=c++1z; g++ -o tuple tuple.o
tuple.cc: In function ‘int main()’:
tuple.cc:15:64: error: no matching function for call to ‘getValue(int, std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >&, std::__cxx11::string&)’
     getValue<std::tuple<std::string>, std::string>(0, t, result);
                                                                ^
tuple.cc:7:13: note: candidate: template<class ... TT, class ReturnType> ReturnType& getValue(int, std::tuple<_Elements ...>&, ReturnType&)
 ReturnType& getValue(int ind, std::tuple<TT...>& t, ReturnType& val) {
             ^
tuple.cc:7:13: note:   template argument deduction/substitution failed:
tuple.cc:15:64: note:   mismatched types ‘std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >’ and ‘std::__cxx11::basic_string<char>’
     getValue<std::tuple<std::string>, std::string>(0, t, result);
                                                                ^

谢谢

getValue(0, t, result);   
这将编译。。。TT不是元组,它是std::string


您试图用…TT作为std::tuple,std::string,…来调用它。。。当然,这与std::string不匹配。

在调试错误时,tuple中还有几个元素被我删除了。所以,如果其中一个参数是tuple,我应该总是调用不带模板的函数而不带模板参数?@unsat不,这不是规则。但如果可以的话,让它来推断。最后一个后续问题:显式参数化模板函数调用会是什么样子?@UnSat这是不可能的;你有一个包后面的模板参数。这对我来说很有趣。在这种情况下,模板参数类型推断有效,但参数类型替换无效。