jeudi 7 février 2019

Why am I getting "no matching function for call to .." for the variadic template function?

I am trying to learn variadic template. I was looking an youtube video where the host had some example. I tried to copy the example and tried compiling but I get error saying "no matching function for call to to_string"

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

template <class T>
std::string to_string_impl(const T &val) {
  std::stringstream ss;
  ss << val;
  return ss.str();
}

template <class t>
std::vector<std::string> to_string() {
  return {};
}

template <typename P1, typename... Param>
std::vector<std::string> to_string(const P1 &p1, const Param &... param) {
  std::vector<std::string> ret;
  ret.push_back(to_string_impl(p1));

  const auto remainder = to_string(param...);
  ret.insert(ret.begin(), remainder.begin(), remainder.end());

  return ret;
}

int main() {
  std::vector<std::string> vec = to_string("Hello", 9.0);
  for (auto v : vec) {
    std::cout << v << std::endl;
  }
}

Aucun commentaire:

Enregistrer un commentaire