简体   繁体   中英

How to make template function call with variadic template parameter?

Below is my simple variadic template function. This template takes std::tuple as one of its input parameter. But it refuses to compile with the error "template argument deduction/substitution failed".

Can anyone please point me to the error I am making?

#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;                                                                                                                                                                                                                                                                                                                                                                                 
}

Below is the compilation output.

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);
                                                                ^

Thanks.

getValue(0, t, result);   

that will compile. ...TT is not a tuple, it is std::string .

You tried to call it with ...TT being std::tuple<std::string>, std::string, ... which of course does not match std::string .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM