vendredi 8 septembre 2017

Getting templated template function declaration correct

I've got a problem like this I am trying to make compile:

#include <vector>
#include <array>

struct Test
{
    template <template <typename...> class Container, typename T, typename... Args>
    void SetData(const Container<T, Args...>& data)
    {
        // compiles for vector but not array
        // 'void Test::SetData(const Container<T,Args...> &)': could not deduce template argument for 'const Container<T,Args...> &' from 'std::array<float,3>' 

    }
};


int main()
{
    Test test;
    std::vector<int> vector{ 1,2,3 };
    std::array<float, 3> arr{1.0f, 2.0f, 3.0f};

    test.SetData(vector);
    test.SetData(arr);

    return 0;
}

Essentially I need a function signature that can accept any arbitary STL container (more specifically vector and array) and I need the type T visible in the function also (i.e int or float in this case). The other types (allocator or array size) I dont care about.

What is the correct signature?

Aucun commentaire:

Enregistrer un commentaire