是否可以推导出std :: insert_iterator的包含类型?

Dre*_*ann 4 c++ iterator sfinae

我有一个期望模板化迭代器类型的函数.

它目前取消引用迭代器来检查正在迭代的类型.

template < typename Iterator >
void func( Iterator i )
{
  // Inspect the size of the objects being iterated
  const size_t type_size = sizeof( *i );

  ...
}
Run Code Online (Sandbox Code Playgroud)

我最近发现了几种标准的迭代器类型,比如std::insert_iterator定义*i为简单的引用i.

也就是说,sizeof(*i)迭代器本身的大小; 与sizeof(i)或相同sizeof(***i)

有没有通用的方法(支持C++ 03)来确定任何标准迭代器迭代的对象的大小或类型?

ipc*_*ipc 5

这就是它iterator_traits的用途.

typedef typename std::iterator_traits<Iterator>::value_type type;
const std::size_t type_size = sizeof(type);
Run Code Online (Sandbox Code Playgroud)

编辑:这不适用于所有输出迭代器.

  • 这很奇怪,[输出迭代器没有值类型](http://stackoverflow.com/questions/2619551/output-iterators-value-type). (2认同)