Skip to content

Header-only mode still includes cassert, causing assert macro to be defined. #2148

Description

@toojays

Consider the following example:

#define FMT_HEADER_ONLY
#include <fmt/format.h>
//#undef assert

struct Test
{
    void assert (bool pass, const char *test_name)
    {
        fmt::print("{}: {}", test_name, pass ? "PASS" : "FAIL");
    }
};

int main()
{
    Test test;
    test.assert(true, "mumble");
    return 0;
}

It fails compilation (godbolt) with:

<source>:7:50: error: macro "assert" passed 2 arguments, but takes just 1
    7 |     void assert (bool pass, const char *test_name)
      |                                                  ^
In file included from /opt/compiler-explorer/gcc-10.2.0/include/c++/10.2.0/cassert:44,
                 from /opt/compiler-explorer/libs/fmt/trunk/include/fmt/format-inl.h:12,
                 from /opt/compiler-explorer/libs/fmt/trunk/include/fmt/format.h:4230,
                 from <source>:2:
/usr/include/assert.h:92: note: macro "assert" defined here
   92 | #  define assert(expr)       \
      | 
<source>:16:31: error: macro "assert" passed 2 arguments, but takes just 1
   16 |     test.assert(true, "mumble");
      |                               ^
In file included from /opt/compiler-explorer/gcc-10.2.0/include/c++/10.2.0/cassert:44,
                 from /opt/compiler-explorer/libs/fmt/trunk/include/fmt/format-inl.h:12,
                 from /opt/compiler-explorer/libs/fmt/trunk/include/fmt/format.h:4230,
                 from <source>:2:
/usr/include/assert.h:92: note: macro "assert" defined here
   92 | #  define assert(expr)       \
      | 
<source>:7:10: error: variable or field 'assert' declared void
    7 |     void assert (bool pass, const char *test_name)
      |          ^~~~~~
<source>:10:5: error: expected ';' at end of member declaration
   10 |     }
      |     ^
      |      ;
<source>: In function 'int main()':
<source>:16:10: error: 'struct Test' has no member named 'assert'
   16 |     test.assert(true, "mumble");
      |          ^~~~~~

As a workaround we can #undef assert immediately after including format.h.

It looks like some work was done to remove the dependency on cassert in 2f9acd1, but this did not extend to the header only mode?

Is it just a matter of replacing the remaining assert() calls with FMT_ASSERT()?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions