When to use the "typename" keyword when using templates

Recently, I have been working on a small project, and I could not understand anything.

I was provided with an .h file containing the class using a name template. Inside this class was a private class.

template <typename T>
class Something
{
public:
        Something();
        ~Something();

        Node* Function1(int index);
        int Index(const T& id);


private:
        class Node()
        {
                public:
                T id;

                //Imagine the rest for the Node


        };      
};

The problem arose when I wanted to define the functions of the class "Something"

This is how I did it (in the .INL file)

template<typename T>
Node* Something::Function1(int index) //Is the return type well written?
{
        // returns the node at the specified index
}

template<typename T>
int Something::Index(const T& id) //Is the parameter type well specified?
{
        // returns the index of the node with the specified id
}

So, the listening part was in the definition part ... Should I tell the compiler that the return type (in this case Node *) uses a name pattern (for example:) typename Node*? What about the parameter? typename const Node&?

So basically, when do I need to indicate that a function / parameter is using a template?

Thank you for your time.

+3
4
template<typename T>
typename Something<T>::Node * Something::Function1(int index) //Is the return type well written?
{
        // returns the node at the specified index
}
+2

Function1 , Node - Something<T>. T ( ), , , typename Something<T>::Node. , T, Something<T>::Node (.. Something<T>).

Index , , - const T& - const T, , T.

+5

: typename , Class::Type, Class . ( Class typedef ..)

: . typename, .

class Outer {
public:
  class Inner {
  };
  Inner* func(Inner* obj);
};

Outer::Inner* func(Inner* obj)
{
}

Inner - Outer::Inner. Inner Outer, func. func Outer, . ( ARE Outer, .

, Outer Something<T>, typename Something<T>::Node.

+3

typename class :

template <class T> class C;

template <typename T> class C;

typename, :

template <typename T> struct A {
    typedef typename T::some_type container;
};
+1
source

Source: https://habr.com/ru/post/1775884/


All Articles