Jiří Lechner
Jiří Lechner

Reputation: 820

Weird template specification

I have run into this function signature

template<typename T>
template<typename FI>
void vector<T>::_M_assign_aux (FI first, FI last,std::forward_iterator_tag)
{}

Is it equivalent to this one?

template<typename T, typename FI>
void vector<T>::_M_assign_aux (FI first, FI last,std::forward_iterator_tag)
{}

Is there any reason to write it separately?

Upvotes: 2

Views: 117

Answers (2)

Rerito
Rerito

Reputation: 6086

Actually, this is a method template for a class template. Therefore, the first template <typename T> applies to vector<T> (the class template). Then, the template <typename FI> applies to the method _M_assign_aux().

If you would gather the whole thing in a unique place it would look like this:

template <typename T>
class vector {
    // Some stuff
    template <typename FI>
    void _M_assign_aux(FI first, FI last, std::forward_iterator_tag) {
        // Some impl
    }
};

Upvotes: 2

rocambille
rocambille

Reputation: 15956

_M_assign_aux looks like a method of vector<T>, so this should have started with a code like this:

template<typename T>
class vector
{
    // ...

    template<typename FI>
    void _M_assign_aux(FI first, FI last,std::forward_iterator_tag);

    // ...
};

Looking class declaration, the explanation is more "visible": you have one template declaration for the class, and one for the method.

template<typename T> // template declaration for vector
template<typename FI> // template declaration for _M_assign_aux
void vector<T>::_M_assign_aux(FI first, FI last,std::forward_iterator_tag)
{
}

Upvotes: 4

Related Questions