Closed cpetig closed 4 years ago
This is where C++ shines! Writing convoluted code to make your interface slightly nicer :)
template <typename T, size_t N>
struct TypedArray {
template <typename... U>
TypedArray(U&&... us) : sz{N}, elem{std::forward<U>(us)...} {}
int sz;
T elem[N];
};
// Deduction via function (C++11)
template <typename T, typename... U>
TypedArray<T, 1 + sizeof...(U)> make_TypedArray(T &&t, U&&... us) {
return TypedArray<T, sizeof...(U) + 1>{ std::forward<T>(t), std::forward<U>(us)... };
}
// Deduction guide (C++17)
template <typename T, typename... U>
TypedArray(T, U...) -> TypedArray<T, 1 + sizeof...(U)>;
int main() {
// C++11
auto ta1 = make_TypedArray(1, 2);
// C++17
TypedArray ta2{ 1.1, 2.2 };
std::cout << ta1.sz << ": " << ta1.elem[0] << " " << ta1.elem[1] << "\n";
std::cout << ta2.sz << ": " << ta2.elem[0] << " " << ta2.elem[1] << "\n";
}
This is where C++ shines! Writing convoluted code to make your interface slightly nicer :)
Wow, I just implemented my original proposal. I will need some time to parse Rafaels proposal 😮
Do I get this correctly that before C++17 you can't make this a compile time evaluated constant (avoiding the actual constructor call at run time)? Since we target embedded I have a bad feeling requiring anything more recent than (or even as recent as) C++11 - since unfortunately there are architectures around with no gcc/llvm support.
With a bit of constexpr, it seems to compile down to constants. In this simple case even without any storage used:
const int hello_tensor_dimension0[3] = { 2, 1,1 }; const struct { int sz; float elem[1]; } hello_quant0_scale = { 1, { 0.0084758047014474869, } };
should become
template <int SZ, class T> struct Array { int sz; T elem[SZ]; }; const Array<2,int> hello_tensor_dimension0 = { 2, {1,1} }; const Array<1,float> hello_quant0_scale = { 1, {0.008475…} };
Bonus: Is there a way to avoid repeating the size while maintaining it being a compile time created structure (which ends up in a text section)? Some C++11 ctor magic with {}?