www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Ugly c++ syntax

reply Rumbu <rumbu rumbu.ro> writes:
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
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
parent Rumbu <rumbu rumbu.ro> writes:
On Saturday, 6 February 2021 at 00:35:12 UTC, Ali Çehreli wrote:
 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.
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#L490
Feb 06 2021
prev sibling parent user1234 <user1234 12.de> writes:
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