lundi 23 mai 2016

C++ how to avoid ambiguous move constructors

I have a class with a couple of fields, assignment c-tor and move c-tor:

class A{
    std::vector<int> numbers;
    int k;


public:

    A(std::vector<int> &&numbers, const int k):
        numbers(numbers), // fast
        k(k)
    {
        // logic
    }

    A(const std::vector<int> &numbers, const int k):
        A(std::move(std::vector<int>(numbers)), k) // copy-and-move vector
    {
        // empty
    }
};

I want to keep logic in one c-tor and call it from others. Also, I want to support fast move-semantics. And I have to explicitly copy-and-move arguments in the assignment c-tor.

Is there any way to avoid such nested construction and keep all advantages I've listed above?

Aucun commentaire:

Enregistrer un commentaire