Portable UNUSED parameter macro used on function signature for C and C++

前端 未结 4 451
终归单人心
终归单人心 2020-11-30 02:20

I\'m interested in creating a macro for eliminating the unused variable warning.

This question describes a way to suppress the unused parameter warning by writing a

4条回答
  •  渐次进展
    2020-11-30 03:08

    Across many compilers I have used the following, excluding support for lint.

    #if (__GNUC__ > 2) || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
    #       define PGM_GNUC_UNUSED         __attribute__((__unused__))
    #else
    #       define PGM_GNUC_UNUSED
    #endif
    

    Tested compilers: GCC, Clang, EKOPath, Intel C Compiler / Composer XE, MinGW32 on Cygwin / Linux / MSYS, MinGW-w64 on Cygwin / Linux, Sun ONE Studio / Oracle Solaris Studio, Visual Studio 2008 / 2010.

    Example usage:

    pgm_tsc_init (
            PGM_GNUC_UNUSED pgm_error_t**   error
            )
    {
    ...
    }
    

    PGM is the standard prefix for this C based project. GNUC is the convention from GLib for this attribute.

    I think one compile warns about __attribute__ in certain circumstances but certainly no error.

提交回复
热议问题