元组和可变参数模板的编译问题

Compile issue with tuples and variadic templates

本文关键字:编译 问题 参数 变参 元组      更新时间:2023-10-16

我遇到了一个看似复杂的问题。

我正在尝试为 zip 函数创建一个迭代器类(试图模仿 python 的生成器 zip 函数)。

我有整个班级在 http://ideone.com/c7rm40

  template<size_t I = 0, typename... Tp>
  inline typename std::enable_if<(I == sizeof...(Tp)), typename std::tuple<decltype(*Tp)...>>::type
  constructImpl(std::tuple<Tp...> const& its) {
core/StarAlgorithm.hpp|550 col 3| error: expected ‘(’ before ‘constructImpl’
core/StarAlgorithm.hpp|550 col 3| error: expected ‘>’ before ‘constructImpl’
core/StarAlgorithm.hpp|550 col 45| error: template argument 2 is invalid
core/StarAlgorithm.hpp|550 col 47| error: expected ‘::’ before ‘{’ token
core/StarAlgorithm.hpp|550 col 47| error: expected identifier before ‘{’ token
core/StarAlgorithm.hpp|550 col 47| error: expected unqualified-id before ‘{’ token

我的问题是,这种方法有效吗? 我不知道为什么它一定是错误的,或者编译器想从我这里得到什么。

但除此之外,如果我缺少更简单的方法,我会很高兴听到它。

我猜问题是*Tp不是decltype的有效表达式。

也许试试declval

std::tuple<decltype(*std::declval<Tp>())...>

或迭代器特征:

 std::tuple<typename std::iterator_traits<Tp>::value_type...>
typename std::tuple<decltype(*Tp)...>>::type

这根本没有意义,因为:

  • Tp是一个类型参数,所以*Tp没有任何意义。

  • std::tuple没有任何嵌套::type。所以std::tuple<whatever>::type没有意义。

根据您的评论,我想您需要std::iterator_traits为:

std::tuple<typename std::iterator_traits<Tp>::value_type...>

希望有帮助。