c++ - How to infer automatically the size of a C-Array within a struct template? -
I have a question about how to automatically estimate the size of the C-arrays within a structure template. Coming from the world of embedded systems, I am interested in romantic strokes to save RAM.
The deal is:
I have many different structures like
straight struct_1 {uint8_t const in variable1; Uint16_t const variable 2; };
In a structure template, I store a c-or holding structure elements and a pointer in the shape of that c-arrow.
template & lt; Typnam T & G; Struct config_data {T const * const ptr_to_c_array; Uint8_t const array; }; Template & lt; Typename t, uint8_t array_size & gt; Constexpr uint8_t getArraySize (T (& amp;) [array_size]) {Back array_ size; }; Int main () {struct_1 const array_1 [] = {{2,5}, {1,9}, {20,20}}; Uint8_t const arraySize_1 = getArraySize (array_1); Config_data & lt; Struct_1 & gt; Const config_data_1 = {array_1, arraySize_1}; }
This is my question:
How do I create a struct template that is automatically shaped using the shape size, without having to explicitly shape my C- Can arrange the size of an array [array] / size (array [0]) or getArraySize template?
I have to do something like this:
template & lt; Typename T, uint8_t array_size & gt; Struct config_data_new {T const * const ptr_to_c_array; Uint8_t const arraySize = array_size; // clear, does not work}; Int main () {struct_1 const config_array_1 [] = {{2,5}, {1,9}, {20,20}}; Config_data_new & lt; Struct_1 & gt; Const config_file_new = {config_array_1}; Uint8_t const size = config_file_new.arraySize; }
Is it possible to estimate the size of the array in template template config_data _new similar to template getArraySize?
You can do something like this:
template & lt; Typename T, std :: size_t N & gt; Constexpr config_data & lt; T & gt; Make-config_data (constant t (and a) [n]) {returns {a, n}; }
Comments
Post a Comment