错误:函数返回类型中推导的类类型为"tuple" [英] error: deduced class type 'tuple' in function return type
问题描述
我在做什么
三年后,我开始学习c ++.我需要快速而广泛地学习,所以我要解决的这个示例对您来说可能看起来很奇怪.
I am practicing c++ after 3 years. I needed to learn fast and broadly, so this example i am trying to solve might look odd to you.
我正在使用 c ++ 20
, gcc 10.2
.
我想制作一个 pythonic枚举函数,该
- 使用任何
container< T>
- 产量
std :: tuple< int,T>
- 其中
T
是容器中项目的类型
- Takes any
container<T>
- Yields
std::tuple<int, T>
- Where
T
is the type of items in the container
我想尝试将 pythonic range 用作 enumerate
其中的一个参数
I wanted to try applying pythonic range as an argument of enumerate
which
- 采用
(int开始,int结束,int步骤)
- 从
start
到end
的每个步骤
生成
int i
范围(不是我的代码,我只添加了 step
功能)
range (Not my code, I only added step
functionality)
template <typename T>
class range_iterator;
template <typename T>
class range_impl
{
const T start_;
const T stop_;
const T step_;
public:
range_impl(T start, T stop, T step) : start_{start}, stop_{stop}, step_{step} {};
range_impl(T start, T stop) : start_{start}, stop_{stop}, step_{1} {};
range_impl(T stop) : start_{0}, stop_{stop}, step_{1} {};
range_iterator<T> begin() const
{
return range_iterator<T>{start_, step_};
}
range_iterator<T> end() const
{
return range_iterator<T>{stop_, step_};
}
};
template <typename T>
class range_iterator
{
T current_;
const T step_;
public:
range_iterator(T init, T step) : current_{init}, step_{step} {};
range_iterator<T> &operator++()
{
current_ += step_;
return *this;
}
bool operator!=(const range_iterator<T> &rhs) const
{
return current_ != rhs.current_;
}
T operator*() const
{
return current_;
}
};
template <typename T>
range_impl<T> range(const T start, const T stop, const T step)
{
return range_impl<T>(start, stop, step);
}
template <typename T>
range_impl<T> range(const T start, const T stop)
{
return range_impl<T>(start, stop);
}
template <typename T>
range_impl<T> range(const T stop)
{
return range_impl<T>(stop);
}
可以如下使用
#include <iostream>
int main()
{
for(auto i: range(0, 100 2)
{
std::cout << i << std::endl;
}
}
问题代码:枚举
template <typename T>
class enumerate_iterator;
// Here, T should be a type of a container that contains type X
template <typename T>
class enumerate_impl
{
T impl;
public:
enumerate_impl<T>(T impl) : impl{impl} {/* empty */};
enumerate_iterator begin() const
{
return enumerate_iterator{impl.begin()};
}
enumerate_iterator end() const
{
return enumerate_iterator{impl.end()};
}
};
// Here, T should be a type of a iterator, I think. Confused myself.
template <typename T>
class enumerate_iterator
{
T iterator;
int i;
public:
enumerate_iterator(T iterator) : iterator{iterator}, i{0} {/* empty body */};
enumerate_iterator<T> &operator++()
{
i++;
iterator++;
return *this;
}
bool operator!=(const enumerate_iterator<T> &rhs) const
{
return iterator != rhs.iterator;
}
std::tuple operator*() const
{
return {i, *iterator};
}
};
template <typename T>
enumerate_impl<T> enumerate(T impl)
{
return enumerate_impl<T>{impl};
}
预期用量
#include <iostream>
int main()
{
for (auto &[i, j] : enumerate(range(0, 100, 2)))
{
std::cout << i << " " << j << std::endl;
}
}
Gotten错误(实际上有很多编译错误,但我想对其他错误进行更多尝试.)
Gotten error (There actually tons of compilation error, but I want to try more on the others).
utility.cpp:186:20: error: deduced class type 'tuple' in function return type
186 | std::tuple operator*() const
我猜想这是在抱怨您没有告诉我元组包含什么类型
.但问题是,我不知道 T迭代器
包含什么类型.我怎么知道返回类型为std :: tuple< int,类型T包含>
?
I guessed this is complaining that you didn't tell me what type the tuple contains
. But the thing is, I don't know what type T iterator
contains. How would I tell return type is std::tuple<int, type T contains>
?
感谢您阅读这个冗长的问题.
Thanks for reading this long long question.
推荐答案
我认为对于 enumerate_iterator
来说,您可能希望其模板类型 T
为";基本元素类型",而不是某些复合迭代类型".
I think that perhaps, for enumerate_iterator
, you might want its template type T
to be a "base element type", not some compound "iteration type".
例如,如果您选择使用原始内存指针来实现迭代,则 enumerate_iterator
的数据成员称为 iterator
的类型将为 T*
(而不是其当前的 T
类型).
For example, if you were to choose to implement your iteration using raw memory pointers, then the enumerate_iterator
's data member called iterator
would have type T*
(instead of its current T
type).
然后,在那种情况下, operator *()
的定义将被编程为具有 std :: tuple< int,T>
And then, in that case, the definition of operator*()
would be programmed as having a return type of std::tuple<int,T>
这篇关于错误:函数返回类型中推导的类类型为"tuple"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!