search

Home  >  Q&A  >  body text

c++ stl traits const T* partial specialization.

as follows:

template <class I>
struct iterator_traits
{
    typedef typename I::value_type   value_type;
}
//针对指向常数对象的指针的特例化
template <class T>
struct iterator_traits<const T*>
{
    typedef T  value_type;
}

I think of getting the value_type related to the iterator. Why convert const int to int? What we want to get is type information. Although the obtained (variable of this type) cannot be modified, why is it said to be useless (said in the stl source code analysis book)

Above, I hope you can help me explain. Thank you.

PHPzPHPz2756 days ago1051

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-05-16 13:32:50

    1. That’s it, the purpose of traits (extraction) is to statically obtain some inherent characteristics of the object at compile time

    2. I don’t quite understand what you want to express. You said “Why convert const int to int”? In fact, const is defined by the following

    template <class T>
    struct iterator_traits<const T*>
    {
        typedef const T  const_value_type;
        //你可以定义更多的,traits出更多的类型
    }

    reply
    0
  • Cancelreply