user3882729
user3882729

Reputation: 1534

non default constructible parameters within lambdas in fold expressions

I'm trying to see if it's possible to use a parameter that is not default constructible within a lambda as part of a fold expression. Following code works if T is default constructible.

template <typename ...T>
struct X
{
   void foo()
   {
    ([](const T&)
    {
       if (sizeof(T) < 4)
       {
          std::cout << "Small block size: " << sizeof(T) << std::endl;
       }
    }(T{}), ...);

    // Print sizes
    ((std::cout << sizeof(T) << std::endl), ...);
   }
};

struct A {
   int a[100];
   };
struct B
{
};

int main()
{
   X<A,B> x;
   x.foo();
}

Output

Small block size: 1
400
1

Upvotes: 0

Views: 42

Answers (1)

HolyBlackCat
HolyBlackCat

Reputation: 96791

The lambda doesn't need a parameter:

([]{
    if (sizeof(T) < 4)
    {
       std::cout << "Small block size: " << sizeof(T) << std::endl;
    }
}(), ...);

Upvotes: 4

Related Questions