vendredi 29 novembre 2019

Create function with unknown number of std::array

I am trying to make a function that pass that test:

TEST(MyCat, CheckOn2Arrays){
    std::array<float, 3> vec1{1.0f, 2.0f, 3.0f};
    std::array<float, 3> vec2{4.0f, 5.0f, 6.0f};

    std::array<float, 6> r = MyCat(vec1, vec2);

    EXPECT_EQ(r[0], 1.0f);
    EXPECT_EQ(r[1], 2.0f);
    EXPECT_EQ(r[2], 3.0f);
    EXPECT_EQ(r[3], 4.0f);
    EXPECT_EQ(r[4], 5.0f);
    EXPECT_EQ(r[5], 6.0f);
}

I have writen that function:

template<class T, size_t N>
auto MyCat((std::array<T, N>) ... arrays) ->
decltype(std::array<T, N*sizeof...(arrays)>) {

    std::array<T, sizeof...(arrays)*N> retArray;

    int i = 0;
    for(std::array<T, N> array: arrays){
        T* = array.begin();
        while(!T){
            retArray[i] = &T;
            i++;
            T++;
        }
    }

    return retArray;
}

The function must take an arbitrary number of arguments and return an object of type std::array. All arguments are of type std::array (T and N are the same for all function arguments).

Im getting that errors and cant understand how to solve them..

../src/test/../myproject/MyCat.h:4:29: error: expected primary-expression before ‘)’ token
auto MyCat((std::array<T, N>) ... arrays) ->
                            ^
../src/test/../myproject/MyCat.h:4:6: warning: variable templates only available with     
std=c++14 or -std=gnu++14
auto MyCat((std::array<T, N>) ... arrays) ->
     ^
../src/test/../myproject/MyCat.h:4:43: error: expected ‘;’ before ‘->’ token
auto MyCat((std::array<T, N>) ... arrays) ->
                                          ^
../src/test/MyCat_test.cc: In member function ‘virtual void     
MyCat_CheckOn2Arrays_Test::TestBody()’:
../src/test/MyCat_test.cc:10:35: error: missing template arguments before ‘(’ token
std::array<float, 6> r = MyCat(vec1, vec2);

Aucun commentaire:

Enregistrer un commentaire