mardi 12 novembre 2019

How to return a template container (i.e. vector, list, array) in a function?

I have the following class:

class Conversion {
//...
public:
    template<class T,
        template<class, class = std::allocator<T>> class> T doTheWork()
    {
        //do the work
        return {};
    }
};

which I want to copy the content to a sequencial container (vector, list, deque), which are declared as:

template<class T, class Allocator = std::allocator<T>>

And I getting confused about the template declaration. Considering I want to have the receiver as the following examples, how should I declare copyContentToContainer?

Examples:

int main() {
    Conversion conv;
    std::vector<std::string> container1 = conv.doTheWork();
    std::list<int> container2 = conv.doTheWork();
    std::deque<double> container3 = conv.doTheWork();
}

Aucun commentaire:

Enregistrer un commentaire