Variadic模板,没有匹配的函数调用

时间:2012-07-09 14:47:34

标签: c++ templates c++11 variadic-templates

我正在编写zip的实现,但我遇到了一些问题。这是一个最小的测试用例:

#include <iostream>
#include <deque>
#include <tuple>
#include <string>
#include <limits>

template <template <typename...> class Container, typename... Types>
Container<std::tuple<Types...>> zip(Container<Types> const&... args) {
  unsigned len = commonLength(args...);
  Container<std::tuple<Types...>> res;
  std::tuple<Types...> item;

  for (unsigned i=0; i<len; i++) {
    item = getTupleFrom(i, args...);
    res.push_back(item);
  }

  return res;
}

template <class ContainerA, class... Containers>
unsigned commonLength(ContainerA first, Containers... rest, unsigned len=std::numeric_limits<unsigned>::max()) {
  unsigned firstLen = first.size();
  if (len > firstLen) {
    len = firstLen;
  }
  return commonLength(rest..., len);
}

template <class ContainerA>
unsigned commonLength(ContainerA first, unsigned len=std::numeric_limits<unsigned>::max()) {
  unsigned firstLen = first.size();
  if (len > firstLen) {
    len = firstLen;
  }
  return len;
}

template <template <typename...> class Container, typename TypeA, typename... Types>
std::tuple<TypeA, Types...> getTupleFrom(unsigned index, Container<TypeA> const& first, Container<Types> const&... rest) {
  return std::tuple_cat(std::tuple<TypeA>(first[index]), getTupleFrom(index, rest...));
}

template <template <typename...> class Container, typename TypeA>
std::tuple<TypeA> getTupleFrom(unsigned index, Container<TypeA> const& first) {
  return std::tuple<TypeA>(first[index]);
}

int main() {

  std::deque<int> test1 = {1, 2, 3, 4};
  std::deque<std::string> test2 = {"hihi", "jump", "queue"};
  std::deque<float> test3 = {0.2, 8.3, 7, 123, 2.3};
  for (auto i : zip(test1, test2, test3)) {
    std::cout << std::get<0>(i) << std::get<1>(i) << std::get<2>(i) << std::endl;
  }
  //expected output:
  //1hihi0.2
  //2jump8.3
  //3queue7
  return 0;
}

编译时出现以下错误:

error: no matching function for call to ‘commonLength(const Star::List<int>&, const Star::List<std::basic_string<char> >&, const Star::List<float>&)’
note: candidates are:
note: template<class ContainerA, class ... Containers> unsigned int Star::commonLength(ContainerA, Containers ..., unsigned int)
note: template<class ContainerA> unsigned int Star::commonLength(ContainerA, unsigned int)

我假设我正在指定我的模板参数错误或类似的东西。我也试图完全重组并消除该功能,但后来我对getTupleFrom得到了同样的错误。

任何人都可以向我解释为什么我愚蠢吗?因为我只是不知道我做错了什么。 :(

2 个答案:

答案 0 :(得分:3)

嗯,这很有效:

#include <iostream>
#include <deque>
#include <tuple>
#include <string>
#include <type_traits>
#include <algorithm>
#include <limits>

template <class ContainerA>
unsigned commonLength(unsigned len, const ContainerA &first) {
  unsigned firstLen = first.size();
  if (len > firstLen) {
    len = firstLen;
  }
  return len;
}


template <class ContainerA, class... Containers>
unsigned commonLength(unsigned len, const ContainerA &first, const Containers&... rest) {
  unsigned firstLen = first.size();
  if (len > firstLen) {
    len = firstLen;
  }
  return commonLength(len, rest...);
}

template <template <typename...> class Container, typename TypeA>
std::tuple<TypeA> getTupleFrom(unsigned index, Container<TypeA> const& first) {
  return std::tuple<TypeA>(first[index]);
}

template <template <typename...> class Container, typename TypeA, typename... Types>
std::tuple<TypeA, Types...> getTupleFrom(unsigned index, Container<TypeA> const& first, Container<Types> const&... rest) {
  return std::tuple_cat(std::tuple<TypeA>(first[index]), getTupleFrom(index, rest...));
}

template <template <typename...> class Container, typename... Types>
Container<std::tuple<Types...>> zip(Container<Types> const&... args) {
  unsigned len = commonLength(std::numeric_limits<unsigned>::max(), args...);
  Container<std::tuple<Types...>> res;
  std::tuple<Types...> item;

  for (unsigned i=0; i<len; i++) {
    item = getTupleFrom(i, args...);
    res.push_back(item);
  }

  return res;
}

int main() {

  std::deque<int> test1 = {1, 2, 3, 4};
  std::deque<std::string> test2 = {"hihi", "jump", "queue"};
  std::deque<float> test3 = {0.2, 8.3, 7, 123, 2.3};
  for (auto i : zip(test1, test2, test3)) {
    std::cout << std::get<0>(i) << std::get<1>(i) << std::get<2>(i) << std::endl;
  }
  //expected output:
  //1hihi0.2
  //2jump8.3
  //3queue7
}

它完全按照您的预期输出。问题是:

  • 你没有在const&中使用commonLength个容器,而zip的参数是const引用。
  • commonLength中的无符号参数无法推断,因此我将其移至开头
  • 您以错误的顺序声明/定义了函数(A必需B,但A在B之前定义),所以我重新排序了它们。

显然clang 3.1无法推断zip中的模板参数,但g ++ 4.6可以很好地推断它们。

答案 1 :(得分:1)

一步一步挑选。

您缺少标题:

#include <limits>

您有未声明的标识符:

template <template <typename...> class Container, typename TypeA, typename... Types>
std::tuple<TypeA, Types...> getTupleFrom(unsigned index, Container<TypeA> const& first, Container<Types> const&... rest) {
  return std::tuple_cat(std::tuple<TypeA>(first[index]), getTupleFrom(index, rest...), end);
}

end在哪里宣布?

你不一致:

Container<std::tuple<Types...>> zip(Container<Types> const&... args) {
  unsigned len = commonLength(args...);
  Container<std::tuple<Types...>> res;

Container<std::tuple<Types...>>还是Container<Types>?或者这正是你的意思?您的代码有点复杂,只需快速查看。

然后,您只有getTupleFrom的{​​{1}}版非{0}的非零数,

Container<TypeA>

这就是

错误的原因
template <template <typename...> class Container, typename TypeA, typename... Types>
std::tuple<TypeA, Types...> getTupleFrom(unsigned index, Container<TypeA> const& first, Container<Types> const&... rest) {
  return std::tuple_cat(std::tuple<TypeA>(first[index]), getTupleFrom(index, rest...), end);
}

template <template <typename...> class Container, typename TypeA>
std::tuple<TypeA> getTupleFrom(unsigned index, Container<TypeA> const& first) {
  return std::tuple<TypeA>(first[index]);
}

表示你以某种方式达到了你的参数列表为空的点(无符号整数参数除外)。我想你需要防止这种情况。