首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用可变模板参数进行模板函数调用?

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

Stack Overflow用户
提问于 2019-03-22 05:25:15
回答 1查看 60关注 0票数 0

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

谁能告诉我我犯的错误是什么?

代码语言:javascript
复制
#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;                                                                                                                                                                                                                                                                                                                                                                                 
}

下面是编译输出。

代码语言:javascript
复制
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);
                                                                ^

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-22 05:29:00

代码语言:javascript
复制
getValue(0, t, result);   

这将会被编译。...TT不是元组,它是std::string

您试图在...TTstd::tuple<std::string>, std::string, ...的情况下调用它,当然这与std::string不匹配。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55289475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档