Search code examples
c++arraystemplatestemplate-argument-deductionctad

Array size template deduction


I have a class that stores an std::array.

The size of the array is evaluated in compile time, this is because the application runs on an embedded device so no dynamic allocations :(. The code looks something like this:

template<uint8_t size>
class A
{
    //some constructor
    A(...);
    std::array<int, size> what;
}
//Wanted use cases
A instance1({1,2,3});
//Unwanted use case
A<3> instance2({1,2,3});

I don't know how to construct the constructor that I want. I've tried for a week now dozens of designs and none got what I wanted. Here are the names of thing that I have tried:

  1. Template deduction guides - also templated versions, which I'm not sure if they are legal...
  2. std::initializer_list - the size of the list cannot be put in the template argument. At least not in a non-constexpr context.
  3. std::array
  4. plain old array
  5. using keyword - also templated.

Tl;Dr:

How to deduce a template parameter value signifying a size of an array type from the given array in the signature of the constructor function?


Solution

  • A small example using deduction guides:

    template<uint8_t size>
    class A
    {
    public:
        template <typename... Args>
        constexpr A(Args&&... args)
        : what { std::forward<decltype(args)>(args)... }
        {}
        constexpr size_t getSize() const { return what.size(); }
    private:
        std::array<int, size> what;
    };
    
    //deduction guide
    template <typename... Args> A(Args... args) -> A<sizeof...(args)>;
    
    int main()
    {
        //Wanted use cases
        A instance1 (1,2,3);
        static_assert (instance1.getSize() == 3);
        
        //or
        //A instance1 {1,2,3};
        return 0;
    }