Forum > General
How to create variable sized arrays within a generic record at compile time
jamie:
Hmm, it seems for the time being I can use this absolute hackery slop code.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TSize10Type = array[1..10] of char; Generic TmyGen <T,Size_T> = Record //..... A:Array[0..SizeOf(Size_T)-1] of T; //...... end; ...Var A:specialize TmyGen<Char, TSize10Type>;
440bx:
--- Quote from: jamie on June 30, 2024, 03:12:32 pm ---Makes it difficult to translate C/C++ code. :(
--- End quote ---
Can you provide an example of the C/C++ code that you are trying to translate ?... maybe there is another way.
Thaddy:
--- Quote from: jamie on June 30, 2024, 05:25:10 pm ---Hmm, it seems for the time being I can use this absolute hackery slop code.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TSize10Type = array[1..10] of char; Generic TmyGen <T,Size_T> = Record //..... A:Array[0..SizeOf(Size_T)-1] of T; //...... end; ...Var A:specialize TmyGen<Char, TSize10Type>;
--- End quote ---
When you are not using trunk that is a work-around. When you are using trunk it is less apropiate use of the new language features.
jamie:
--- Quote from: 440bx on June 30, 2024, 05:29:38 pm ---
--- Quote from: jamie on June 30, 2024, 03:12:32 pm ---Makes it difficult to translate C/C++ code. :(
--- End quote ---
Can you provide an example of the C/C++ code that you are trying to translate ?... maybe there is another way.
--- End quote ---
Go here
https://learn.microsoft.com/en-us/cpp/cpp/templates-cpp?view=msvc-170
scroll down to "Non Type Parameters"
That is what I am dealing with.
If it were just a couple here and there I would hard code it but these templates are being used through out the app with lots of different sizes.
There is more than one array per unit for each generic (template) that references this size.
This is a large CAD program and I already tried using dynamic methods in the translation which seem to be working but I noticed a slowdown and the static methods of doing this seems to work much faster. There is lots of code doing complete structure copies from one to the other, etc.
I am not even sure at the completion of this, if the compiler will handle all of the translations without a memory elapse!
Thaddy:
It is definitely not the non-type parameters, since this is exactly that:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode delphi}type TmyArray<T; const lowbound,highbound:Integer>= Record a:Array[lowbound..highbound] of T; end; var a:TMyArray<integer,3,14>;begin writeln('nr of elements is: ',length(a.a)); writeln('bounds are: ', low(a.a):4,high(a.a):4);end.There is a limitation as opposed to C++17 and higher, though.
Sven wrote this about it:
--- Code: Text [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---"Important: Unlike C++ FPC does not support default specializations, thus doing recursive specializations together with operations on constants will result in out of stack exceptions or infinite loops:"
But then the C++ code must explicitly use that last feature.
Before C++17 it was also not supported in C++.
What you probably mean is "Default template arguments"
The link is:
https://lists.freepascal.org/pipermail/fpc-devel/2020-April/042708.html
Navigation
[0] Message Index
[#] Next page
[*] Previous page