Dependent name

  • 개념

    • template paramter에 의존하는 이름
    • non-type / type / template 중 하나로 해석
  • 상황

    • 아래와 같이 그냥 T::으로 적으면 cpp 컴파일러들은 non-type으로 해석함
    • T::type같이 type이면 typename이라고 붙여줘야 한다
      struct Object
    {
        using type = int;
        static constexpr int value = 10;
        template<typename T> struct rebind
        {
        };
    };
    
    template<typename T>
    void foo(T obj)
    {
        // Object::value * 10; // 10 * 10
        // Object::type * p1; // int* p1
        // Object::rebind<int> a; //
        T::value * 10; // 10 * 10
        typename T::type * p1; // int* p1
        typename T::template rebind<int> a; //
    }
    
    int main()
    {
        Object obj;
        foo(obj);
    }
      
  • 상황 2

      template<typename T> struct Object
    {
        using type = int;
        template<typename U>
        void mf() { }
    };
    
    template<typename T>
    void foo()
    {
        // Object<int>::type t1;
        typename Object<T>::type t1;
        // Object<int> obj1;
        Object<T> obj1;
        obj1.template mf<int>(); // obj1 T template
    }
    int main()
    {
        foo<int>();
    }
      

value type

  • 개념
    • STL의 모든 컨테이너에는 value_type이라는 멤버 타입이 있다.
  • 예시
      template<typename T>
    void print_first_element(const T& v)
    {
        // typename T::value_type n = v.front();
        auto n = v.front();
        std::cout << n << std::endl;
    }