safe_numerics icon indicating copy to clipboard operation
safe_numerics copied to clipboard

Consider enabling CTAD for the safe type

Open correaa opened this issue 3 months ago • 0 comments

CTAD: class template argument deduction

Such thing will allow this use case, in C++20 and higher.

#include <boost/safe_numerics/safe_integer.hpp>

void uncontrolled(double*, int);  // true external function

using boost::safe_numerics::safe;

int main() {

    std::vector<double> v = {1.0, 2.0, 3.0};

    uncontrolled(v.data(), safe<int>{v.max_size()});  // ok, throws

//  uncontrolled(v.data(), safe{v.max_size()});  // needs CTAD
}

complete code: https://godbolt.org/z/s8bYz9rqb

The most basic code for this feature could be:

#if define(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201703L)
namespace boost::safe_numerics {
     template<class In> safe_base(In) -> safe_base<In>;
}
#endif

correaa avatar Oct 03 '25 20:10 correaa