https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125948

            Bug ID: 125948
           Summary: Inconsistent -Wold-style-cast
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nikita.leontiev at gmail dot com
  Target Milestone: ---

Code:

void func(int) {}

#define CALL(func, arg) \
        CALL_##func((arg))

#define CALL_FUNC1(arg) \
        func((int)(arg))

#define CALL_FUNC2(arg) \
        func((int)arg)

int main()
{
        CALL(FUNC1, 0);
        CALL(FUNC2, 0);
        return 0;
}

gcc generated 2 warning:

main.cpp: In function 'int main()':
main.cpp:7:23: warning: use of old-style cast to 'int' [-Wold-style-cast]
    7 |         func((int)(arg))
      |                       ^
main.cpp:4:9: note: in expansion of macro 'CALL_FUNC1'
    4 |         CALL_##func((arg))
      |         ^~~~~
main.cpp:14:9: note: in expansion of macro 'CALL'
   14 |         CALL(FUNC1, 0);
      |         ^~~~
main.cpp:4:25: warning: use of old-style cast to 'int' [-Wold-style-cast]
    4 |         CALL_##func((arg))
      |                         ^
main.cpp:10:19: note: in definition of macro 'CALL_FUNC2'
   10 |         func((int)arg)
      |                   ^~~
main.cpp:15:9: note: in expansion of macro 'CALL'
   15 |         CALL(FUNC2, 0);
      |         ^~~~

Moving CALL macro to a header and using as a system header:

g++ -isystem include -Wold-style-cast -o test main.cpp

There is only one warning left:

main.cpp: In function 'int main()':
main.cpp:6:23: warning: use of old-style cast to 'int' [-Wold-style-cast]
    6 |         func((int)(arg))
      |                       ^
include/helper.h:2:9: note: in expansion of macro 'CALL_FUNC1'
    2 |         CALL_##func((arg))
      |         ^~~~~

Modifying CALL macro: parentheses around arg are removed:

#define CALL(func, arg) \
        CALL_##func(arg)

Both warnings are generated again:

main.cpp: In function 'int main()':
main.cpp:6:23: warning: use of old-style cast to 'int' [-Wold-style-cast]
    6 |         func((int)(arg))
      |                       ^
include/helper.h:2:9: note: in expansion of macro 'CALL_FUNC1'
    2 |         CALL_##func(arg)
      |         ^~~~~
main.cpp:14:21: warning: use of old-style cast to 'int' [-Wold-style-cast]
   14 |         CALL(FUNC2, 0);
      |                     ^
main.cpp:9:19: note: in definition of macro 'CALL_FUNC2'
    9 |         func((int)arg)
      |                   ^~~

Reply via email to