C++11: defining function on std::array

前端 未结 2 842
栀梦
栀梦 2021-01-14 10:18

std::array takes two template parameters:

typename T // the element type
size_t N // the size of the array

I want to define a

相关标签:
2条回答
  • 2021-01-14 10:39

    The N needs to be a static value. You can, e.g., make a template argument:

    template <std::size_t N>
    void f(std::array<char, N> x) {
        ...
    }
    

    In your example, I would still pass the argument by reference, though:

    template <std::size_t N>
    void f(std::array<char, N> const& x) {
        ...
    }
    
    0 讨论(0)
  • 2021-01-14 11:00

    Use a template function:

    template<size_t N> void f(array<char, N> x) {
    }
    
    0 讨论(0)
提交回复
热议问题