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:
- Template deduction guides - also templated versions, which I'm not sure if they are legal...
std::initializer_list
- the size of the list cannot be put in the template argument. At least not in a non-constexpr context.std::array
- plain old array
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?
A small example using deduction guides: