Does boost have an assert that works nicely with constexpr evaluation, and if not should we add one?
As you may know assert does not play nicely with constexpr evaluation. But std::is_constant_evaluated/if consteval gives us a way to check when the execution happens. https://www.godbolt.org/z/dfder8h16 Does something like this exist in boost, and if not would that be a nice addition? I can not figure out how to support line numbers, filename in a same way assert does, since assert will obviously just take the line info from where it is called. P.S. smart_assert is just a first name I thought of, I am fine with any name as long as it is not some lengthy monstrosity like is_constant_evaluated
Ivan Matek wrote:
As you may know assert does not play nicely with constexpr evaluation.
It does. https://www.godbolt.org/z/KhPja6cEs
On Sat, Aug 21, 2021 at 5:56 PM Peter Dimov via Boost
It does. https://www.godbolt.org/z/KhPja6cEs
Ah my mistake. :( Do you then maybe know why boost clamp has commented out the assert in implementation? https://github.com/boostorg/algorithm/blob/c9077bd49580bddfaa8a5c71abc4022a2... I presumed it is to avoid problems with assert and constexpr, but as you now I learned assert works fine since C++14.
P.S. I think gcc might have been reason why I misremembered how assert does
or does not work in constexpr, so maybe
that is the reason why it is commented out in clamp?
It was fixed few years ago(GCC 9).
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86678
On Sat, Aug 21, 2021 at 9:05 PM Ivan Matek
On Sat, Aug 21, 2021 at 5:56 PM Peter Dimov via Boost < boost@lists.boost.org> wrote:
It does. https://www.godbolt.org/z/KhPja6cEs
Ah my mistake. :( Do you then maybe know why boost clamp has commented out the assert in implementation?
https://github.com/boostorg/algorithm/blob/c9077bd49580bddfaa8a5c71abc4022a2... I presumed it is to avoid problems with assert and constexpr, but as you now I learned assert works fine since C++14.
On 21/08/2021 16:54, Peter Dimov via Boost wrote:
Ivan Matek wrote:
As you may know assert does not play nicely with constexpr evaluation. It does. https://www.godbolt.org/z/KhPja6cEs
My thought too. -- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus
On 8/21/21 11:54 AM, Peter Dimov via Boost wrote:
Ivan Matek wrote:
As you may know assert does not play nicely with constexpr evaluation.
It does. https://www.godbolt.org/z/KhPja6cEs
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
What am I missing here? I click the above link and see the code If I compile with gcc x86-64 trunk, I get the following:
source>: In function 'int main()': <source>:12:21: error: non-constant condition for static assertion 12 | static_assert(5 == func(9,1)); | ~~^~~~~~~~~~~~ In file included from /opt/compiler-explorer/gcc-trunk-20210825/include/c++/12.0.0/cassert:44, from <source>:2: <source>:12:28: in 'constexpr' expansion of 'func(9, 1)' <source>:6:5: error: call to non-'constexpr' function 'void __assert_fail(const char*, const char*, unsigned int, const char*)' 6 | assert(a
Thanks
David P. Riedel wrote:
On 8/21/21 11:54 AM, Peter Dimov via Boost wrote:
Ivan Matek wrote:
As you may know assert does not play nicely with constexpr evaluation.
It does. https://www.godbolt.org/z/KhPja6cEs
What am I missing here?
I click the above link and see the code
If I compile with gcc x86-64 trunk, I get the following:
source>: In function 'int main()': <source>:12:21: error: non-constant condition for static assertion 12 | static_assert(5 == func(9,1)); | ~~^~~~~~~~~~~~ In file included from /opt/compiler-explorer/gcc-trunk- 20210825/include/c++/12.0.0/cassert:44, from <source>:2: <source>:12:28: in 'constexpr' expansion of 'func(9, 1)' <source>:6:5: error: call to non-'constexpr' function 'void __assert_fail(const char*, const char*, unsigned int, const char*)' 6 | assert(a
That's because the assertion fails. a=9, b=1.
On 8/25/21 2:43 PM, Peter Dimov wrote:
David P. Riedel wrote:
On 8/21/21 11:54 AM, Peter Dimov via Boost wrote:
Ivan Matek wrote:
As you may know assert does not play nicely with constexpr evaluation. It does. https://www.godbolt.org/z/KhPja6cEs
What am I missing here?
I click the above link and see the code
If I compile with gcc x86-64 trunk, I get the following:
source>: In function 'int main()': <source>:12:21: error: non-constant condition for static assertion 12 | static_assert(5 == func(9,1)); | ~~^~~~~~~~~~~~ In file included from /opt/compiler-explorer/gcc-trunk- 20210825/include/c++/12.0.0/cassert:44, from <source>:2: <source>:12:28: in 'constexpr' expansion of 'func(9, 1)' <source>:6:5: error: call to non-'constexpr' function 'void __assert_fail(const char*, const char*, unsigned int, const char*)' 6 | assert(a
Doh (smacks head)
Thank you ! 😁
participants (4)
-
David P. Riedel
-
Ivan Matek
-
John Maddock
-
Peter Dimov