How to generalize container adaptors with templates?

145 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

Consider your calling code:

a.data()

There is nothing here for data() to deduce its return type. You have to precise it explicitly, something like:

a.data<std::stack>()

But, from your comments, you can't edit the usage code. What you can do is using a template type conversion operator:

template <typename M>
operator M() const
{
    M other_cont ( typename M::container_type ( cont.begin(), cont.end() ) );
    return other_cont;
}

To keep the code unedited, your data method should return this object:

const MySet<T> data() const
{
    return *this;
}
3
On

The simple approach is to use a conversion operator:

#include <set>
#include <stack>
#include <queue>
#include <string>

template <typename T>
class MySet
{
    public:
        template <typename O, typename = typename O::container_type>
        operator O() const
        {
            return O(typename O::container_type(cont.begin(), cont.end()));
        }

    private:
        std::set<T> cont;
};

Using this approach the notation is different, though: there is no data() member being used:

int main()
{
    MySet<std::string> s;
    std::stack<std::string> st = s;
    std::queue<std::string> qu = s;
} 

If you want to use a data() member and get different types out of the result, you'll need to return a proxy which converts appropriately upon access:

#include <set>
#include <stack>
#include <queue>
#include <string>

template <typename T>
class MySet
{
    public:
        class Proxy {
            std::set<T> const* set;
        public:
            Proxy(std::set<T> const* s): set(s) {}
            template <typename O, typename = typename O::container_type>
            operator O() const
            {
                return O(typename O::container_type(set->begin(), set->end()));
            }
        };

        Proxy data() const { return Proxy{&this->cont}; }
    private:
        std::set<T> cont;
};

int main()
{
    MySet<std::string> s;
    std::stack<std::string> st = s.data();
    std::queue<std::string> qu = s.data();
}