Array Creation and Initialization #2238
brianoh
started this conversation in
Language design
Replies: 2 comments 1 reply
-
You can already almost do this in C++:
In other words, the two problems that make this not work in C++ are that ordinary function parameters can't be treated as compile-time constants, and arrays aren't copyable. Carbon's design already fixes the first probem, and I expect that our design for arrays (once we have one) will fix the second problem. |
Beta Was this translation helpful? Give feedback.
1 reply
-
Thanks for the info.
…On Sat, Oct 1, 2022 at 12:39 AM Geoff Romer ***@***.***> wrote:
const char * arcBackspace = fnFillArray(29, '\b', 1); // SIZE, FILL CHAR, ADD TERMINATOR.
You can already almost do this in C++:
std::array<char> arcBackspace = fnFillArray<29>('\b', 1);
In other words, the two problems that make this not work in C++ are that
ordinary function parameters can't be treated as compile-time constants,
and arrays aren't copyable. Carbon's design already fixes the first probem
<https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/README.md#checked-and-template-parameters>,
and I expect that our design for arrays (once we have one) will fix the
second problem.
—
Reply to this email directly, view it on GitHub
<#2238 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAWYALKU5EOGVWP7ZBMP4TWA4JUXANCNFSM6AAAAAAQZWVYNQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
.com>
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Array creation and Initialization in Function and related.
Currently in c++, AFAIK, an array cannot be created and returned from a Function unless the size of the array is a constant literal value expressed in the Function, as opposed to a constant value passed to the Function, or even a constant literal passed to the Function. I have found this a little inconvenient on a small scale, but I presume that it could have wider implications. I presume that the reason for this is that it is difficult for the compiler to track the array size value, even though it is a constant, and even a constant literal passed to the Function. Others have encountered this problem. Where I encountered this was in defining a simple char array that looked rather messy, even though it takes only one line to define, so I thought that having a tiny generalized Function to handle it would look better, and could be used elsewhere, but I discovered that it is not currently possible in C++ using G++ and MSVC both on Windows.
Another possibility would be to allow the creation using a similar syntax to the current one but with a different result.
EG: char arcBackspace[29] = {‘\b’}, or int ariVals[100] = {1} to initialize the whole array to the specified value. I did read that for the compiler to handle this it would create it at compile time and potentially create a very large executable. That may not be a good solution, however, very large arrays probably should not be created in this way, so it may not be an issue. The other aspect with the char array is that it would need the option of ‘\0’ termination. Compatibility with existing code obviously needs to be maintained. The solution to creating and returning an array in a function by passing the size of the array could be adequate. I note that std::array has a fill function, but AFAIK it cannot create and fill the array with one statement, and in this case, I think it would take 3 statements.
I presume that Carbon will have a different architecture to handle arrays, so I thought that it may be worthy of mentioning these issues. Obviously, they are not major issues, but, a lot of minor improvements can make a major difference, and it’s not much use raising them after the design is finished.
The following is what I wanted to replace using a generalized Function, because it is messy IMO. It is good insofar as it provides the termination character (IE: 28 '\b' and terminator provided).
I would like to be able to create it with one statement without typing all of the characters to fill the array, eg:
The following is an example of my attempted test solution that is invalid because “int 29” is not a constant literal value defined in the Function creating the array, so it is of little use to me in that format. I did read somewhere that the size needs to be passed as a pointer to a const int, but that didn't work either.
Beta Was this translation helpful? Give feedback.
All reactions