C++ Logo

std-proposals

Advanced search

C++ Function argument constraints for argument sanity checking

From: Jim Smith <jim45002_at_[hidden]>
Date: Thu, 1 Aug 2019 16:59:54 +0000
Functions sometimes check the sanity of the data passed to them. This could
require a number of checks and error handing. For example:

Foo& Foo::operator=(const Foo& obj)
{
  if(&obj == this)
      return *this
   ...
}

A more convenient way of handling this situation would be to allow
constraints on arguments and let the compiler check or add code to handle
the condition.

For example:

Foo& Foo::operator=(const Foo& obj { &obj != this } ) <-- Argument
constraint in braces
{
   ...
}

Possibly throwing an "argument constraint" exception for run-time
evaluations and failing to compile or compiler warning for compile-time
evaluations.


--Jim Smith

Received on 2019-08-01 12:02:05