← Home ← Back to /g/

Thread 107147609

47 posts 10 images /g/
Anonymous No.107147609 [Report] >>107147678 >>107147719 >>107148380 >>107148909 >>107149751 >>107150428 >>107150486 >>107151706
std::array<int, 3> arr = {4, 2, 0};
for (using elem_t = decltype(arr)::value_type; elem_t i : arr) {
std::cout << i << endl;
}
s0ychan No.107147626 [Report] >>107147672
what would be the purpose of writing like that?
Anonymous No.107147665 [Report] >>107147672
yes, OP, your code is shit. why do you code like that?
Anonymous No.107147672 [Report] >>107147706 >>107147716
>>107147626
>>107147665
It's the newest thing in town. Daddy Bjarne told me it's the way.
Anonymous No.107147678 [Report] >>107147699 >>107147721 >>107147740
>>107147609 (OP)
>not importing classes into scope with using std::array;
>not using std::println in 2025
>why is c++ so ugly?
go back to c tranny
Anonymous No.107147699 [Report]
>>107147678
>>not using std::println in 2025
C++ trannies reinvented printf?
Anonymous No.107147704 [Report]
also
>creating a fake type to iterate over instead of just iterating over int[] as a fucking int
>using _t suffixes
kys cnile trannoid
Anonymous No.107147706 [Report] >>107147713
>>107147672
>Daddy Bjarne told me it's the way.
sauce?
Anonymous No.107147713 [Report] >>107147768
>>107147706
https://en.cppreference.com/w/cpp/language/range-for.html
Anonymous No.107147716 [Report]
>>107147672
Technically you can remove the = assignment operator for constant initialization since you already have {}
Anonymous No.107147719 [Report] >>107147737 >>107147799 >>107149121
>>107147609 (OP)
Usecase for std::array and C++?
In C, this is just
int arr[] = {4, 2, 0};
for (size_t i = 0; i < sizeof arr / sizeof *arr; i++)
printf("%i\n", arr[i]);
Anonymous No.107147721 [Report] >>107147731
>>107147678
>::printf le good even though you cant create custom formatters for it
just join the 41% already, you will never be a woman
Anonymous No.107147731 [Report] >>107147746
>>107147721
usecase for custom formatters?
Anonymous No.107147737 [Report]
>>107147719
>int a[] = {4, 2, 0}
in normal (non-retarded) languages this is int[] a
Anonymous No.107147740 [Report] >>107147746
>>107147678
println is some module bullshit and will tank your compiler performance. Not worth it imo.
Anonymous No.107147746 [Report] >>107147754
>>107147731
use case for high level languages? go write everything in assembly larping retard

>>107147740
headers tank your compile times tenfold compared to modules you retarded cnile jeet
Anonymous No.107147754 [Report] >>107147766
>>107147746
>high level is when I overcomplicate my code for the lulz
Anonymous No.107147766 [Report]
>>107147754
>saar, re-inventing HashMap every time i program in c makes me smart! java is le verbose and bad, even though in c i have to re-write my own libraries every time i program which no one is impressed by
the absolute state of cnilejeets
Anonymous No.107147768 [Report] >>107147779
>>107147713
>chooses the most contrived variant
>somehow Bjarne told him to do so
kys
Anonymous No.107147779 [Report] >>107147870
>>107147768
If it's in there, it means it's heckin cute and valid, you bigot.
Anonymous No.107147787 [Report] >>107147796 >>107147799 >>107149928 >>107150397
Can someone explain to me what is wrong with OP's code other than the fact it's C++23?
Anonymous No.107147796 [Report]
>>107147787
I wrote endl instead of std::endl. Won't compile. It's joever.
Anonymous No.107147799 [Report] >>107147816
>>107147787
Far more verbose than >>107147719.
Anonymous No.107147816 [Report] >>107147823
>>107147799
It's not the same code though. It's not a fair comparison.
Anonymous No.107147823 [Report]
>>107147816
They do the exact same thing.
Anonymous No.107147870 [Report] >>107147886
>>107147779
it is valid. and your straw man code is trash.
Anonymous No.107147886 [Report] >>107147957
>>107147870
>straw man code
In what way is it strawman? Your first reaction to it is “this code is trash”. Why would a language even bother adding this syntax if the user’s first reaction is “wtf is this shit”?
Anonymous No.107147957 [Report] >>107147980 >>107148506
>>107147886
>In what way is it strawman?
because you obviously could use for (auto i : arr), you disingenuous retard
>Why would a language even bother adding this syntax
to be able to declare a symbol whose visibility is restricted to the scope of the loop. afaik they added it to match init part of if-statements.
Anonymous No.107147980 [Report] >>107148241
>>107147957
Yeah, or I could use OP code. Or the 30 other completely valid declarations. It all compiles and does the job.

Who said your way is any better? Try arguing that on a team project. There's always going to be some ashkyally guy who'll tell how how his declaration is superior because of some obscure feature introduced in C++17 that's like totally better.
scabPICKER No.107148241 [Report]
>>107147980
>does the job
What do you think your job is?
Anonymous No.107148380 [Report]
>>107147609 (OP)
haha OP I love froggo XD
Anonymous No.107148506 [Report]
>>107147957
>because you obviously could use for (auto i : arr), you disingenuous retard
This.

But I guess troon's gonna troon until they 41% themselves. Sooner's better than later, of course.
Anonymous No.107148528 [Report]
Haskell fixes this
Anonymous No.107148909 [Report] >>107149121 >>107149733 >>107150506
>>107147609 (OP)
Didn't know you could put a 'using' inside a for loop. Apparently you can. Has to be C++23 though.
Anonymous No.107149121 [Report] >>107149773
>>107148909
>580ms to compile one for loop
Grim.
This >>107147719 compiles in 30ms.
Anonymous No.107149733 [Report]
>>107148909
>Didn't know you could put a 'using' inside a for loop.
No, that's older than that, I have seen it in books and I haven't read anything C++ in almost a decade.
Anonymous No.107149751 [Report]
>>107147609 (OP)
The code you provided is a C++ program that uses a range-based for loop with a type variable to iterate over an std::array<int, 3> object named arr. Let's break down the code and understand its functionality:

cpp

std::array<int, 3> arr = {4, 2, 0};
This line declares and initializes an std::array named arr with 3 elements. The elements are initialized with the values 4, 2, and 0.
cpp

for (using elem_t = decltype(arr)::value_type; elem_t i : arr) {
This is a range-based for loop that iterates over the elements of arr.
decltype(arr)::value_type is used to deduce the type of each element in the array. In this case, it will be int because arr is an std::array<int, 3>.
elem_t is a type variable that is used as the type for the loop variable i.
i is the loop variable that will be used to access each element of arr in each iteration.
cpp

std::cout << i << endl;
This line prints the value of each element i in the range-based for loop.
cpp

}
This line marks the end of the range-based for loop.
When you run this code, it will output:

text

4
2
0
The range-based for loop iterates over each element of arr and prints its value using the loop variable i.

In summary, the code declares an std::array with three elements and then iterates over each element using a range-based for loop, printing each element on a separate line.

Statistics:
40.07 tokens/s
381 tokens
9.51s
Anonymous No.107149773 [Report]
>>107149121
It's not one for loop, assuming it's libstdc++ #include <iostream> alone pulls 2 dozen headers and thousands of lines of templates
Anonymous No.107149928 [Report]
>>107147787
You shouldn't be calling endl in a loop, it will flush output on every iteration and it's expensive.

Just put a newline at the end instead.
Anonymous No.107150397 [Report] >>107151197
>>107147787
you can just
std::array<int, 3> arr {4, 2, 0};
for (auto& i : arr)
{
std::cout << i << std::endl;
}
Anonymous No.107150428 [Report] >>107151197
>>107147609 (OP)
why not just
std::array<int, 3> arr = {4, 2, 0};
for (int i : arr) {
std::cout << i << endl;
}
Anonymous No.107150456 [Report]
you know you can just
int arr[] = {4, 2, 0};
for(int i=0; i<sizeof(arr)/sizeof(*arr); i++){
printf("%d\n", arr[i]);
}
Anonymous No.107150486 [Report] >>107151197
>>107147609 (OP)
for (auto i : {4, 2, 0})
std::cout << i << '\n';
Anonymous No.107150506 [Report]
>>107148909
>Has to be C++23 though.
Nailed it.
Anonymous No.107151197 [Report] >>107151259
>>107150397
>>107150428
>>107150486
>does he know?
Anonymous No.107151259 [Report]
>>107151197
idgi
Anonymous No.107151706 [Report]
>>107147609 (OP)
say$_,for(4,2,0)
even Perl, le writeonly lang, is more legible than sepples.