Skip to content

Add FMT_CUSTOM_ASSERT_FAIL - #4505

Merged
vitaut merged 1 commit into
fmtlib:masterfrom
HazardyKnusperkeks:weak-assert
Aug 25, 2025
Merged

Add FMT_CUSTOM_ASSERT_FAIL#4505
vitaut merged 1 commit into
fmtlib:masterfrom
HazardyKnusperkeks:weak-assert

Conversation

@HazardyKnusperkeks

@HazardyKnusperkeks HazardyKnusperkeks commented Jul 29, 2025

Copy link
Copy Markdown
Contributor

That way one can provide ones own implementation for assert_fail, which is moved out of the detail namespace. For binary compatibility the detail version stays to call the outer version.

@HazardyKnusperkeks

Copy link
Copy Markdown
Contributor Author

I want to use libfmt in a bare metal project, with exceptions disabled and I don't want to pull in any of the printf-familiy, so I need my own implementation. Instead of a weak symbol one could hide the implementation behind an #if (e.g. FMT_USER_PROVIDED_ASSERT_FAIL), if that's more to your liking.

@HazardyKnusperkeks

Copy link
Copy Markdown
Contributor Author

I want to use libfmt in a bare metal project, with exceptions disabled and I don't want to pull in any of the printf-familiy, so I need my own implementation. Instead of a weak symbol one could hide the implementation behind an #if (e.g. FMT_USER_PROVIDED_ASSERT_FAIL), if that's more to your liking.

Seeing the failing tests, that seems to be needed?

@HazardyKnusperkeks HazardyKnusperkeks changed the title Mark detail::assert_fail as weak symbol Add FMT_USER_PROVIDED_ASSERT_FAIL Jul 29, 2025
@vitaut

vitaut commented Aug 1, 2025

Copy link
Copy Markdown
Contributor

Thanks for the PR but I don't think we should add yet another configuration macro. You can already control this via FMT_ASSERT / FMT_THROW.

@vitaut vitaut closed this Aug 1, 2025
@HazardyKnusperkeks

Copy link
Copy Markdown
Contributor Author

I actually can not really. Because the functions I'd like to be called is not yet in scope, when fmt/format.h is included, even if using the classic assert, obviously the same for my own functions.

/opt/bin/g++ -g -O0 -Wall -Wextra -m64 -pipe -fexceptions -fvisibility=default -fPIC -DFMT_USE_NOEXCEPT '-DFMT_ASSERT(c,t)=assert(c)' -I.../libs/3rdParty/fmt/include -std=c++2b -o.../build/Desktop-Debug/Debug_Desktop_038b678e9426a45b/fmt.875c51b4/f5439ee4c1a5e132/os.cc.o -c .../libs/3rdParty/fmt/src/os.cc
.../libs/3rdParty/fmt/include/fmt/format.h: In function 'constexpr fmt::v11::detail::uint128_fallback fmt::v11::detail::operator*(const uint128_fallback&, uint32_t)':
<command-line>: error: 'assert' was not declared in this scope
.../libs/3rdParty/fmt/include/fmt/format.h:325:5: note: in expansion of macro 'FMT_ASSERT'
  325 |     FMT_ASSERT(lhs.hi_ == 0, "");
      |     ^~~~~~~~~~
In file included from .../libs/3rdParty/fmt/include/fmt/os.h:11,
                 from .../libs/3rdParty/fmt/src/os.cc:13:

But declaring it as weak, if and only if FMT_HEADER_ONLY is not defined would also work for me.