我有以下课程:
#include <set>
#include <stack>
#include <queue>
#include <string>
template <typename T>
class MySet
{
public:
const std::stack<T> data() const
{
std::stack<T> other_cont ( typename std::stack<T>::container_type ( cont.begin(), cont.end() ) );
return other_cont;
}
private:
std::set<T> cont;
};
以及以下代码:
MySet<std::string> a;
MySet<int> b;
const std::stack<std::string> s = a.data();
const std::queue<int> q = b.data();
我想使用一个模板化成员函数来初始化任何适配器类型。到目前为止,它只适用于stack
or queue
,我不知道如何使用模板来概括它。
这是我尝试过的:
template <template <typename> typename M>
const M<T> data() const
{
M<T> other_cont ( typename M<T>::container_type ( cont.begin(), cont.end() ) );
return other_cont;
}
编译器说它不能推导出模板参数M
。