【问题标题】:Compile issue with tuples and variadic templates使用元组和可变参数模板编译问题
【发布时间】:2013-01-26 19:31:47
【问题描述】:

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

我正在尝试为 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

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

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

【问题讨论】:

  • typename std::tuple&lt;decltype(*Tp)...&gt; 不应该有typename。不知道这是否是问题。
  • 为什么decltype(*Tp) 中有*?你为什么要使用decltype 开头? Tp本身就是一个类型。
  • 我把它添加进来作为尝试的东西,我已经中途破解了一段时间,只是为了排除一些事情。它也不适用于删除 typename
  • @Nawaz,因为这个函数接受一个不同类型的迭代器的元组,解引用它们,并返回一个解引用类型的元组。
  • @OmnipotentEntity:decltype 不适用于 type。和*Tp 甚至没有意义! Tp 不是指针值,是类型!

标签: c++ tuples variadic-templates template-meta-programming decltype


【解决方案1】:

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

也许试试declval

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

或迭代器特征:

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

【讨论】:

  • declval 不起作用,但 iterator_traits 效果很好。谢谢。
【解决方案2】:
typename std::tuple<decltype(*Tp)...>>::type

这根本没有意义,因为:

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

  • std::tuple 没有任何嵌套的::type。所以std::tuple&lt;whatever&gt;::type 没有意义。

根据您的 cmets,我 您需要 std::iterator_traits 为:

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

希望对您有所帮助。

【讨论】:

  • 删除指针的问题在于 AFAIK 它只适用于指针,一般不适用于迭代器。
  • @Pubby:好的。在这种情况下,std::iterator_traits 会起作用!
猜你喜欢
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
  • 2011-09-28
  • 2019-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多