user_48349383
user_48349383

Reputation: 517

std::pair as a template<class> argument? C++

I have a class that is a template, using the argument: template <class X>

Can I template this class to be of type std::pair < W, Z>? I am getting an unresolved external symbol error, and trying to track down the cause.

Upvotes: 1

Views: 4379

Answers (1)

John Humphreys
John Humphreys

Reputation: 39254

A little example of passing a pair into a template.

#include <iostream>
#include <vector>

template <typename T>
class C {
    public:
        void add(const T& val) { m_vec.push_back(val); }
    private:
        std::vector<T> m_vec;
};

int main()
{
    C<std::pair<int, char> > pairC;
    pairC.add(std::make_pair(5, 2));
}

would instantiate a template class taking a std::pair and holding it in a vector. Add inserts a pair made with make_pair into that vector.

Note that for older C++ compilers you need to add a space between the two right-chevrons to stop it from being seen as a right-shift operator.

Upvotes: 3

Related Questions