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:
std::initializer_list - the size of the list cannot be put in the template argument. At least not in a non-constexpr context.std::arrayusing 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:
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;
}
#include <array>
template<class... U>
class A
{
std::array<int,sizeof...(U)> what;
public:
constexpr A(U&&... u) : what{std::forward<U>(u)...} { }
constexpr std::size_t size() const {
return what.size();
}
};
int main() {
A a(1,2,3,4,5,6);
static_assert(a.size() == 6);
A b(1);
static_assert(b.size() == 1);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With