I've got the following class:
#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;
};
And the following code:
MySet<std::string> a;
MySet<int> b;
const std::stack<std::string> s = a.data();
const std::queue<int> q = b.data();
I would like to use one templated member function to initialize any adaptor type.
So far it only works with either stack
or queue
, I can't figure out how to use templates to generalize it.
This is what I've tried:
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;
}
Compiler says it can't deduce the template parameter M
.
Consider your calling code:
There is nothing here for
data()
to deduce its return type. You have to precise it explicitly, something like:But, from your comments, you can't edit the usage code. What you can do is using a template type conversion operator:
To keep the code unedited, your
data
method should return this object: