# How to pass a compile-time constant number to a generic lambda

**URL:** https://discourse.itk.org/t/how-to-pass-a-compile-time-constant-number-to-a-generic-lambda/4763
**Category:** Engineering
**Created:** [January 28, 2022, 2:42pm UTC](https://discourse.itk.org/t/how-to-pass-a-compile-time-constant-number-to-a-generic-lambda/4763 "2022-01-28T14:42:43Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [January 28, 2022, 5:17pm UTC](https://discourse.itk.org/t/how-to-pass-a-compile-time-constant-number-to-a-generic-lambda/4763/4 "2022-01-28T17:17:24Z")

</div>

Thanks to you both for your feedback @dzenanz, @blowekamp

A use case for such generic lambda’s that I find interesting is a specific unit test that tests the same requirements for _1D_, _2D_, and _3D_ (or _ND_, in general). For example, the following lambda, `checkSizeFill`, would test `itk::Size<dimensionValue>::Fill` for any dimension.

```auto
TEST(Size, Fill)
{
  const auto checkSizeFill = [](const auto dimensionConstant) {

    for (const itk::SizeValueType sizeValue : { 0, 1, 2, INT_MAX })
    {
      itk::Size<dimensionConstant> actualSize;
      actualSize.Fill(sizeValue);
      const auto expectedSize = itk::Size<dimensionConstant>::Filled(sizeValue);
      EXPECT_EQ(actualSize, expectedSize);
    }
  };

  // Check itk::Size::Fill for 1D, 2D, and 3D:
  checkSizeFill(std::integral_constant<unsigned int, 1>{});
  checkSizeFill(std::integral_constant<unsigned int, 2>{});
  checkSizeFill(std::integral_constant<unsigned int, 3>{});
}

```

This `checkSizeFill` still compiles fine with VS2019. Those buggy compile errors only appear when the generic lambda gets somewhat more complicated!

Note that we can make it still more beautiful by introducing an `itk::DimensionConstant` alias, to allow writing:

```auto
  // Check itk::Size::Fill for 1D, 2D, and 3D:
  checkSizeFill(itk::DimensionConstant<1>{});
  checkSizeFill(itk::DimensionConstant<2>{});
  checkSizeFill(itk::DimensionConstant<3>{});

```

Do you like it? 😃

* * *

Edit: Removed `decltype(dimensionConstant)::value` from the example – it appears unnecessary 😃

---

_[View the full topic](https://discourse.itk.org/t/how-to-pass-a-compile-time-constant-number-to-a-generic-lambda/4763)._
