Question

First of all, here are my examples:

SymmetricMatrix<std::vector<int>, int> a;
SymmetrixMatrix<std::list<int>, int> b;
SymmetricMatrix<std::deque<double>, double> c;

SymmetricMatrix<std::vector<int> > d;
SymmetricMatrix<std::vector<double> > e;

So there are two template parameters. If I write a default parameter for the second one, I do not have to name it when I create the object. But how can I adjust it to the first parameter? For example if the first parameter is std::vector<int>, the second one should be int, if the first one is std::vector<double>, the second one should be double.

Was it helpful?

Solution

template<class DS, class T = typename DS::value_type>
class SymmetricMatrix
{
};

SymmetricMatrix<std::deque<double>> c;

Assumes that DS (data structure) contains typedef value_type, or it won't compile. You can replace value_type with whatever you want, so long as it's in DS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top