Custom Container emplace with variadic templates

风流意气都作罢 提交于 2020-01-17 04:11:27

问题


I am implementing a simple circular vector class. I want to implement an emplace member function, but I am getting an error which I don't understand. It might be a simple fix for something I am doing wrong, but since I don't have much experience with variadic templates, I can't figure out what...

The error I am getting is:

main.cpp: In function 'int main()':

main.cpp:104:50: error: no matching function for call to 'CircularVector<Item>::emplace(int, int, int, int, int, int, std::vector<int>, int)'
     v.emplace(1, 0, 1, 0, 1, 0, vector<int>(), -1);
                                                  ^

main.cpp:104:50: note: candidate is:

main.cpp:20:7: note: void CircularVector<T, Args>::emplace(const Args& ...) [with T = Item; Args = {}]
  void emplace(const Args &... args) {
       ^

main.cpp:20:7: note:   candidate expects 0 arguments, 8 provided

The source code generating this error is (also located here http://coliru.stacked-crooked.com/a/37d50d6f23363357):

#include <vector>

using namespace std;

#define CIRCULAR_BUFFER_DEFAULT_SIZE 5000

template <typename T, typename ...Args>
class CircularVector {
public:
    CircularVector(int size) {
        _size = size;
        _v.reserve(_size);
    }

    CircularVector() {
        _size = CIRCULAR_BUFFER_DEFAULT_SIZE;
        _v.reserve(_size);
    }

    void emplace(const Args &... args) {
        ++Count;
        ++_indexWrite;
        if (_indexWrite > _size - 1) _indexWrite = 0;
        _v.emplace(_indexWrite, args...);
    }

    void push(const T& item) {
        ++Count;
        ++_indexWrite;
        if (_indexWrite > _size - 1) _indexWrite = 0;
        _v[_indexWrite] = item;
    }

    void pop(T& item) {
        item = _v[_indexRead];
        ++_indexRead;
        if (_indexRead > _size - 1) _indexRead = 0;
        --Count;
    }

    T& back() {
        return _v[(_indexRead + Count - 1) % _size];
    }


    void erase(int numItems) {
        _indexRead += numItems;
        if (_indexRead > _size - 1) _indexRead -= _size;
        Count -= numItems;
    }

    void eraseAt(int index) {
        swap(_v[index], _v[(_indexRead + Count - 1) % _size]);
        --Count;
        --_indexWrite;
        if (_indexWrite < 0) {
            _indexWrite = _size - 1;
        }
    }

    void clear() {
        _indexRead = 0;
        _indexWrite = -1;
        Count = 0;
    }


    T& operator[](std::size_t idx) {
        int index = _indexRead + idx;
        if (index > _size) index = index % _size;
        return _v[index];
    };

    int Count = 0;

private:
    int _indexWrite = -1;
    int _indexRead = 0;
    int _size = 0;

    std::vector<T> _v;
};


class Item {
public:
    double A;
    int B;
    int C;
    vector<int> D;
    int E;

    Item(double a, int b, int c, vector<int> &d, int e) {
        A = a;
        B = b;
        C = c;
        D = d;
        E = e;
    }
};

int main() {
    CircularVector<Item> v;
    v.emplace(1, 0, 1, 0, 1, 0, vector<int>(), -1);
}

回答1:


In case anyone stumbles upon the same issue, this is how I achieved this:

void emplace(Args&&... args) {
    ++Count;
    ++_indexWrite;
    if (_indexWrite > _size - 1) _indexWrite = 0;
    _v.emplace(_v.begin() + _indexWrite, std::forward<Args>(args)...);
}

Although what I really wanted was to construct an element using the reserved memory in that index, and not inserting a new element at that specific position.




回答2:


Args has to be a template parameter of emplace, not CircularVector.

template <typename T>
class CircularVector {
public:

    /* ... */

    template<typename ...Args>
    void emplace(const Args &... args) {
        /* ... */
    }
};

Also, you should consider using forwarding references for emplace.



来源:https://stackoverflow.com/questions/28628484/custom-container-emplace-with-variadic-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!