digitalmars.D.learn - Ugly c++ syntax
- Rumbu (8/8) Feb 05 2021 Can some C++ guru translate in D the template below?
- =?UTF-8?Q?Ali_=c3=87ehreli?= (25/30) Feb 05 2021 With some guesses, I made the following compile:
- Rumbu (5/9) Feb 06 2021 Thank you Ali, but nope, it's "parameter pack folding". This
- user1234 (2/10) Feb 06 2021 modern cpp looks like 10 lines of decl for 1 of actual code.
Can some C++ guru translate in D the template below? I gave up after reading a lot, but I didn't manage to understand the meaning "&& ..." template <typename...Tables> static uint8_t composite_index_size(Tables const&... tables) { return (composite_index_size(tables.size(), impl::bits_needed(sizeof...(tables))) && ...) ? 2 : 4; } Thanks.
Feb 05 2021
On 2/5/21 1:10 PM, Rumbu wrote:I gave up after reading a lot, but I didn't manage to understand the meaning "&& ..."I think it's the universal reference.template <typename...Tables> static uint8_t composite_index_size(Tables const&... tables) { return (composite_index_size(tables.size(), impl::bits_needed(sizeof...(tables))) && ...) ? 2 : 4; }With some guesses, I made the following compile: import std.meta : staticMap; import std.algorithm : sum; ubyte composite_index_size(tables...)() { return (composite_index_size(tables.length, bits_needed(staticMap!(SizeOf, typeof(tables))))) ? 2 : 4; } auto composite_index_size(size_t count, size_t total) { return 42; } enum SizeOf(T) = T.sizeof; auto bits_needed(size_t[] args...) { return args.sum; } void main() { int[] i; double[] d; pragma(msg, composite_index_size!(i, d)); } Ali
Feb 05 2021
On Saturday, 6 February 2021 at 00:35:12 UTC, Ali Çehreli wrote:On 2/5/21 1:10 PM, Rumbu wrote:Thank you Ali, but nope, it's "parameter pack folding". This allows starting with C++ 17 to have recursive templates in one liner. Basically, it means "&& template(the rest of arguments)" https://github.com/microsoft/cppwin32/blob/8a6b2507734dd9e9892059b5794f35109688fc20/cppwin32/winmd/impl/winmd_reader/database.h#L490I gave up after reading a lot, but I didn't manage to understand the meaning "&& ..."I think it's the universal reference.
Feb 06 2021
On Friday, 5 February 2021 at 21:10:21 UTC, Rumbu wrote:Can some C++ guru translate in D the template below? I gave up after reading a lot, but I didn't manage to understand the meaning "&& ..." template <typename...Tables> static uint8_t composite_index_size(Tables const&... tables) { return (composite_index_size(tables.size(), impl::bits_needed(sizeof...(tables))) && ...) ? 2 : 4; } Thanks.modern cpp looks like 10 lines of decl for 1 of actual code.
Feb 06 2021