我可以将向下转换应用于迭代器吗?

问题描述

我需要知道我是否通过应用向下转换向模板类发送了正确类型的容器。

// 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;
}

我收到错误

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);

无论如何我可以检查容器中迭代器的类型,以便我可以控制它是否会在模板类中传递?

解决方法

看起来您完全误解了标准库中的迭代器。首先,如果您发现自己使用任何标准库类型执行 dynamic_cast(一个显着的例外 - he-he,双关语 - 是 std::exception,也许还有一些我没有想到的其他类型)现在)你做错了什么。绝大多数 STL 类型都不是多态的,不应如此使用。

迭代器也是如此。您不需要将 std::vector::iterator 强制转换为 biderectional_iterator,而只需在需要此类迭代器的地方使用它。有 iterator_traits,当与向量迭代器一起使用时,将表明向量的迭代器通过 iterator_category 是双向的。查看有关 iterator_traits

的更多信息

traits 的美妙之处在于所有这些逻辑都是在编译时完成的,所以如果你试图在迭代器中使用它无法处理的场景,你最终会出现编译错误,并且可以立即修复程序.

,

dynamic_cast 对实例化的对象执行运行时检查。

模板解析(主要)适用于类型(而非对象),并在编译时发生。一个类型的编译时检查的例子是 std::is_base_of<Base,Type>

vector<int>::iterator i1;

using iterator_type = std::iterator_traits<decltype(i1)>::iterator_category;
if( std::is_base_of_v<std::bidirectional_iterator_tag,iterator_type> )
// Can be "if constexpr" above,since this is known at compile time
{
  cout << "Succesfull" << endl;