I need to know if I am sending a template class the right type of containers by applying downcast.
// C++ program to demonstrate input iterator
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> v1 = { 1, 2, 3, 4, 5 };
// Declaring an iterator
vector<int>::iterator i1;
bidirectional_iterator & bi = dynamic_cast<i1&>(i1);
if(bi != nullptr)
cout << "Succesfull" << endl;
for (i1 = v1.begin(); i1 != v1.end(); ++i1) {
// Accessing elements using iterator
cout << (*i1) << " ";
}
return 0;
}
I get the error:
prog.cpp: In function ‘int main()’:
prog.cpp:12:2: error: ‘bidirectional_iterator’ was not declared in this scope
bidirectional_iterator & bi = dynamic_cast<i1&>(i1);
^
prog.cpp:12:27: error: ‘bi’ was not declared in this scope
bidirectional_iterator & bi = dynamic_cast<i1&>(i1);
^
prog.cpp:12:45: error: ‘i1’ does not name a type
bidirectional_iterator & bi = dynamic_cast<i1&>(i1);
^
prog.cpp:12:47: error: expected ‘>’ before ‘&’ token
bidirectional_iterator & bi = dynamic_cast<i1&>(i1);
^
prog.cpp:12:47: error: expected ‘(’ before ‘&’ token
prog.cpp:12:48: error: expected primary-expression before ‘>’ token
bidirectional_iterator & bi = dynamic_cast<i1&>(i1);
^
prog.cpp:12:53: error: expected ‘)’ before ‘;’ token
bidirectional_iterator & bi = dynamic_cast<i1&>(i1);
Is there anyway I can check the type of iterator in a container so I can control whether it will be passed in a templated class?