Search code examples
c++c++11templatestemplate-argument-deduction

std::array with unknown length as a type for template function specification


I want to pass std::array class with a known type, but unknown size to the template function specialization:

class foo {
public:
void bar<class T>(T arg1, int arg2, int arg3) {
    //For any other T
}
void bar<std::array<rs::float3, size>>(T arg1, arg2) { 
    //Specific function for type std::array of rs::float3's and unknown length (size is not a type, just variable).
}

Solution

  • What about something like

    template <typename T>
    void foo (T const &)
     { std::cout << "foo generic" << std::endl; }
    
    template <std::size_t Dim>
    void foo (std::array<float, Dim> const &)
     { std::cout << "foo float array dim " << Dim << std::endl; }
    

    ?

    The following is a full working example

    #include <array>
    #include <iostream>
    
    template <typename T>
    void foo (T const &)
     { std::cout << "foo generic" << std::endl; }
    
    template <std::size_t Dim>
    void foo (std::array<float, Dim> const &)
     { std::cout << "foo floar array dim " << Dim << std::endl; }
    
    int main ()
     {
       foo(0);
       foo(std::array<float, 12U>{}); 
     }