문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top