[英]Compile issue with tuples and variadic templates
我遇到了一个看似复杂问题的问题。
我正在尝试为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...>
希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.