vendredi 1 avril 2016

Perfect forwarding for methods of non-template classes

Perfect forwarding is usually seen in the context of template classes. For a non-template class, is it worth making e.g. the constructor a template method so it can use perfect forwarding? Something like the below:

class Foo()
{
    public:
        template<typename T>
        Foo(T &&vec) : memberVec(std::forward<T>(vec)) {};

    private:
        std::vector memberVec;
};

The advantages are fundamentally the same, but is anything different when we know the real class type? When would this be good practice and when not?

Aucun commentaire:

Enregistrer un commentaire