Reputation: 159
I have a template class below that depends on 2 classes U
and T
and have implemented the iterator function for both classes (see the end of this post).
I can iterate over the respective vectors of the class using iterators,
but I was wondering if it was possible to do the same using the for range loop syntax instead of using iterators.
With something like
for (auto x : myclass <double>)
{
std::cout << x << std::endl ;
}
I know it does not work but I could not manage to find the syntax if any, Thanks in advance for your answer
#include<iostream>
#include<string>
#include<vector>
template<class U,class T>
class MyClass
{
public:
MyClass(
const std::vector<U> & vect_u ,
const std::vector<T> & vect_t )
{
m_vect_u = vect_u;
m_vect_t = vect_t;
}
~MyClass(){}
// begin()
template<class Z>
typename std::enable_if<std::is_same<Z,T>::value, typename std::vector<Z>::iterator>::type
begin() noexcept { return m_vect_t.begin(); }
template<class Z>
typename std::enable_if<std::is_same<Z,U>::value, typename std::vector<Z>::iterator>::type
begin() noexcept { return m_vect_u.begin(); }
// end()
template<class Z>
typename std::enable_if<std::is_same<Z,T>::value, typename std::vector<Z>::iterator>::type
end() noexcept { return m_vect_t.end(); }
template<class Z>
typename std::enable_if<std::is_same<Z,U>::value, typename std::vector<Z>::iterator>::type
end() noexcept { return m_vect_u.end(); }
// cbegin()
template<class Z>
typename std::enable_if<std::is_same<Z,T>::value, typename std::vector<Z>::const_iterator>::type
cbegin() const noexcept { return m_vect_t.cbegin(); }
template<class Z>
typename std::enable_if<std::is_same<Z,U>::value, typename std::vector<Z>::const_iterator>::type
cbegin() const noexcept { return m_vect_u.cbegin(); }
// cend()
template<class Z>
typename std::enable_if<std::is_same<Z,T>::value, typename std::vector<Z>::const_iterator>::type
cend() const noexcept { return m_vect_t.cend(); }
template<class Z>
typename std::enable_if<std::is_same<Z,U>::value, typename std::vector<Z>::const_iterator>::type
cend() const noexcept { return m_vect_u.cend(); }
private:
std::vector<U> m_vect_u ;
std::vector<T> m_vect_t ;
};
int main()
{
std::vector<double> vect_double = {1.5,2.5,3.5} ;
std::vector<int> vect_int = {-1,-2,-3} ;
MyClass<double,int> myclass( vect_double, vect_int);
std::cout << "iteration over int" << std::endl ;
for(auto itr = myclass.begin<int>(); itr < myclass.end<int>() ; ++itr)
{
std::cout << *itr << std::endl ;
}
std::cout << "iteration over double" << std::endl ;
for(auto itr = myclass.begin<double>(); itr < myclass.end<double>() ; ++itr)
{
std::cout << *itr << std::endl ;
}
return 0 ;
}
Upvotes: 2
Views: 965
Reputation: 218323
You might provide function to return "range" (directly std::vector
or a wrapper):
template <typename Z> // Or SFINAE or if constexpr or any other implementation
auto& getVector() { return std::get<std::vector<Z>&>(std::tie(m_vect_u, m_vect_t)); }
template <typename Z>
const auto& getVector() const { return std::get<const std::vector<Z>&>(std::tie(m_vect_u, m_vect_t)); }
and then
std::cout << "iteration over int" << std::endl ;
for (auto e : myclass.getVector<int>())
{
std::cout << e << std::endl ;
}
Upvotes: 4
Reputation: 123548
You need an object that has non-templated begin
and end
. For example a class template that wraps MyClass
:
template <typename Z,typename U,typename T>
struct MyClassWrapper {
MyClass<U,T>* parent;
auto begin() { return parent-> template begin<Z>(); }
auto end() { return parent-> template end<Z>();}
};
template <typename Z,typename U,typename T>
MyClassWrapper<Z,U,T> make_wrapper(MyClass<U,T>* p){
return {p};
}
Then this works:
for (auto& x : make_wrapper<int>(&myclass)){
std::cout << x << "\n";
}
for (auto& x : make_wrapper<double>(&myclass)){
std::cout << x << "\n";
}
Upvotes: 4