c++ - 可变参数模板中的模板参数推导失败

标签 c++ c++11 templates variadic-templates template-argument-deduction

我正在编写一个函数,可以将字符串 vector 的内容倒入变量中。这是它的样子:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

template <typename T, int N = 0>
void pour2(std::vector<std::string> const& vals, T& val) {
    std::stringstream ss; 
    ss << vals[N];
    ss >> val;
}

template <typename T, typename ...Ts, int N = 0>
void pour2(std::vector<std::string> const& vals, T& val, Ts& ...args) {
    std::stringstream ss; 
    ss << vals[N];
    ss >> val;
    pour2<Ts..., N+1>(vals, args...);
}


int main() {

    std::vector<std::string> info = {"3", "1.5", "/home/tq/playground/"};

    int sz; 
    double val;
    std::string dir;

    pour2(info, sz, val, dir);

    std::cout << "size = " << sz << std::endl;
    std::cout << "value = " << val << std::endl;
    std::cout << "dir = " << dir << std::endl;

    return 0;
}

但是,g++-9.2 提示说
test.cpp: In instantiation of ‘void pour2(const std::vector<std::__cxx11::basic_string<char> >&, T&, Ts& ...) [with T = int; Ts = {double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >}; int N = 0]’:
test.cpp:30:26:   required from here
test.cpp:18:19: error: no matching function for call to ‘pour2<double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, (0 + 1)>(const std::vector<std::__cxx11::basic_string<char> >&, double&, std::__cxx11::basic_string<char>&)’
   18 |  pour2<Ts..., N+1>(vals, args...);
      |  ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
test.cpp:7:6: note: candidate: ‘template<class T, int N> void pour2(const std::vector<std::__cxx11::basic_string<char> >&, T&)’
    7 | void pour2(std::vector<std::string> const& vals, T& val) {
      |      ^~~~~
test.cpp:7:6: note:   template argument deduction/substitution failed:
test.cpp:18:19: error: wrong number of template arguments (3, should be at least 1)
   18 |  pour2<Ts..., N+1>(vals, args...);
      |  ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
test.cpp:14:6: note: candidate: ‘template<class T, class ... Ts, int N> void pour2(const std::vector<std::__cxx11::basic_string<char> >&, T&, Ts& ...)’
   14 | void pour2(std::vector<std::string> const& vals, T& val, Ts& ...args) {
      |      ^~~~~
test.cpp:14:6: note:   template argument deduction/substitution failed:


和 clang-9.0.1 说
test.cpp:18:2: error: no matching function for call to 'pour2'
        pour2<Ts..., N+1>(vals, args...);
        ^~~~~~~~~~~~~~~~~
test.cpp:30:2: note: in instantiation of function template specialization 'pour2<int, double, std::__cxx11::basic_string<char> , 0>' requested here
        pour2(info, sz, val, dir);
        ^
test.cpp:14:6: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'Ts'
void pour2(std::vector<std::string> const& vals, T& val, Ts& ...args) {
     ^
test.cpp:7:6: note: candidate function template not viable: requires 2 arguments, but 3 were provided
void pour2(std::vector<std::string> const& vals, T& val) {
     ^
1 error generated.

我发现如果我将非类型模板参数移动为第一个参数,则代码可以按预期编译和工作:
template <int N = 0, typename T>
void pour(std::vector<std::string> const& vals, T& val) {
    std::stringstream ss; 
    ss << vals[N];
    ss >> val;
}

template <int N = 0, typename T, typename ...Ts>
void pour(std::vector<std::string> const& vals, T& val, Ts& ...args) {
    std::stringstream ss; 
    ss << vals[N];
    ss >> val;
    pour<N+1, Ts...>(vals, args...);
}

我只是想知道,为什么它在第一种情况下不起作用?

最佳答案

如果 N 出现在参数包之后,则 N 需要从函数参数中推导出来或具有默认参数,并且 N 不能由函数参数推导。

"In a function template, the template parameter pack may appear earlier in the list provided that all following parameters can be deduced from the function arguments, or have default arguments". - [parameter_pack - cppreference]

关于c++ - 可变参数模板中的模板参数推导失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60332744/

相关文章:

c++ - 应用程序启动后如何隐藏命令提示符?

c++ - NDK : has not been declared

C++11构造函数继承和纯虚方法

c++ - 可变参数模板函数的仿函数

templates - 比较液体中的日期

c++ - 如何从 QListView 中搜索和选择项目?

c++ - 使用 C 和 C++ 绑定(bind)链接到库

c++ - c++ 中的引用是内存中的一个新位置(如指针)还是它所绑定(bind)的对象的同一个位置?

c++ - C++ 中 "using"关键字背后的逻辑是什么?

c++ - 模板函数和 const 限定符中的类型推导