Using constexpr to check literal parameters in a constructor

I started experimenting with constexpr

.
What I'm trying to achieve is to validate the literal

numerical values โ€‹โ€‹presented as ctor parameters.
I started with the following, casting when plotting MyStruct

with a value <= 4.

constexpr int validate(int v)
{
  return (v > 4) ? v : throw exception();
};

struct MyStruct final
{
  constexpr MyStruct(const int v)
    : _v{validate(v)}
  {
  }

  void add(int toAdd)
  {
    _v += toAdd;
  }

  int _v;
};

int main(int argc, char**)
{
  constexpr MyStruct a{500};  // ok so far...
  a.add(argc);                // ...nope 
  MyStruct b{500};            // check at runtime :(
  MyStruct c{argc};           // runtime check ok
}

      

The labeling MyStruct

both constexpr

works as expected, but it prevents the call add

since it hasn't changed.

I think it can be done as I am only targeting literal values โ€‹โ€‹(known at compile time).
I would like to avoid templates.

+3


source to share


1 answer


Function arguments are not const expression: - /

You can pass std::integral_constant<std::size_t, 4u>

to allow compile-time checking inside the constructor:

struct MyStruct final
{
    // For runtime or constexpr usage.
    constexpr MyStruct(int v) : _v{validate(v)} {}

    // For compile-time values
    template <std::size_t N>
    constexpr MyStruct(std::integral_constant<std::size_t, N>) : _v{N}
    {
        static_assert(N > 4, "Unexpected");
    }

    // ...
};

      



and then

MyStruct b{std::integral_constant<std::size_t, 500>{}};

      

+5


source







All Articles