C++ Logo

std-discussion

Advanced search

Re: Extended overflow arithmetic library

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Fri, 19 Jan 2024 09:20:31 +0000
Hi Giuseppe,

It was an attempt at humor, not as "bashing".

Sorry in advance if I am misunderstanding the interpretation of the standard.

I understand that the standard states that only certain types should be allowed, but the issue specifically is how the signature is defined.
If one were to follow that definition to the letter the following should be available:

template< class T >
constexpr T add_sat( T x, T y ) noexcept;

but if that ever happens wouldn't that be game over? A library implementer can then specialize the listed types that would then be visible to the translation unit,
but as far as I know there's no requirement that would prevent an overload in a non-constexpr context where the implementation is not visible from being used.

Take for example the following code:

#include <cstdint>
#include <iostream>

template <class T>
constexpr T do_something(T p_out);

int main(
 [[maybe_unused]] int argc,
 [[maybe_unused]] char** argv,
 [[maybe_unused]] char** envp)
{
 int32_t var = do_something(argc);
 std::cout << var << std::endl;
 return 0;
}

This will compile everywhere despite the fact that the function do_something has 0 specializations, it will fail to link, but never fails to compile.
A library implementer can still constrain the types, but then their declaration cannot look like the one above.
The standard may say it is constrained, but the signature in the standard itself contradicts that.
Yes, we can still find ways to do it with specific compiler features, but wouldn't it be better if the signature itself reflected that?
Or am I misunderstanding something?

Best regards,
Tiago



-----Original Message-----
From: Std-Discussion <std-discussion-bounces_at_lists.isocpp.org> On Behalf Of Giuseppe D'Angelo via Std-Discussion
Sent: Friday, 19 January 2024 01:09
To: std-discussion_at_[hidden]pp.org
Cc: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Subject: Re: [std-discussion] Extended overflow arithmetic library

Il 18/01/24 23:32, Tiago Freire via Std-Discussion ha scritto:
> Hi everyone,
>
> I would like to submit a draft paper related to the introduction of
> functions in the <numeric> library related to overflow arithmetic in
> the same spirit of the “saturated” function family.
>
> Feedback is appreciated; you can have an early read of the text here:
> https://kaotic.software/cpp_papers/overflow_arithmetic.html
> <https://kaotic.software/cpp_papers/overflow_arithmetic.html>

I don't understanding the "bashing" over the saturation functions.
The paper goes for some easy irony:

> If only there was a way to define in the template parameters which overloaded types were allowed? Oh, wait there is! It is called concepts, and it has been introduced in C++20, we should use that.

Guess what? All the saturating function templates are already constrained on T being integral:

https://eel.is/c++draft/numeric.sat


My 2 c,

--
Giuseppe D'Angelo

Received on 2024-01-19 09:20:33