3 ms·
It can also be simpler if you use idiomatic modern C++ with std::array<T, n> and std::function<R(T1, T2)>.
by kccqzy 10y ago
It can also be simpler if you use idiomatic modern C++ with std::array<T, n> and std::function<R(T1, T2)>.
- Jach 10y agoOr Nim: var str: array[10, ptr byte] (and much richer types) Edit: and while I'm here, Nim has other sensible syntax for this low level stuff... var b: byte = 10 str[0] = addr b echo $str[0][]
- userbinator 10y agoLooking at "idiomatic modern C++", I am often at a loss for words at what lengths they've gone to in order to reinvent things while greatly obfuscating them in the process. Is there a std::pointer_to<T> too? I don't know, but something like this std::array<std::pointer_to<byte>, 10> str; certainly does not look any more readable to me than byte *str[10]; . (Disclaimer: I mainly work with C, but find some C++ features genuinely useful, although the majority of the time they seem more like absurd complexity for the sake of complexity.)
- detrino 10y agostd::array does not exist because it is easier to read. It exists because C arrays behave strangely. Two examples: decay to pointer and no value semantics.
- physguy1123 10y agoI've never seen nor heard of pointer_to ever being used to declare a pointer to something. I believe t's used inside of custom allocators for a generic type that might not use a normal pointer as the pointer type, but would never be used for normal declarations like this. std::array is useful for letting the compiler avoid array-to-pointer decaying, value semantics, and also actually putting array length type info in a function parameter.