mardi 12 février 2019

How do I pass all vector elements separated by comma into function in C++?

I have been reading this website for quite a while now but have just registered. I have also used search which did not seem to be very helpful.

Here it is: As I am having fun with C++ I come along "lazy evaluation" conception on the Internet. I am interested in creating something like a "lazy vector" which is defined with the function which takes N arguments and first N vector elements.

However, I have currently come across the issue with it. Let me point it out:

template<typename T, typename... A>
class llazy
{

public:
    llazy(const std::function<T(A...)>& func, A... args) : func(func), vec({args...}), numArgs(sizeof...(args))
    {}
    const T& operator[](size_t index)
    {
        unsigned short tmp = 1;
        //Here I want to stray from 2 elements to pass to function and make the number at least like 0-4
        std::vector<size_t> v;
        for (unsigned short i = 0; i < numArgs; i++)
            v.emplace_back(vec.size() - tmp++);
        //As you can see here, there are no issues(if the vector has enough elements ofc) with these two
        unsigned long long i = vec.size() - 2, j = vec.size() - 1;
        while (vec.size() < index + 1)
            //So how do I pass all the vec[v[0]], vec[v[1]], etc... elements to the "func"?
            //Maybe there is another way to achieve this, however, this is the Python which makes me think this is possible
            vec.emplace_back(func(vec[i++], vec[j++]));
        if (vec.size() >= index + 1)
            return vec[index];
    }
private:
    const unsigned char numArgs;
    const std::function<T(A...)> func;
    std::vector<T> vec;
};

Thank you for your answers in advance.

Aucun commentaire:

Enregistrer un commentaire