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

时间:2019-03-21 21:25:15

标签: 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& 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);
                                                                ^

谢谢。

1 个答案:

答案 0 :(得分:1)

getValue(0, t, result);   

将会编译。 ...TT不是元组,而是std::string

您尝试用...TT作为std::tuple<std::string>, std::string, ...来调用它,当然它与std::string不匹配。