[[maybe_unused]] and Constructors

2020-08-17 18:09发布

问题:

Trying to compile the sqlpp17 codebase with gcc 8.2.1 and clang 6.0.1 have been a really strange experience. The code pushes the compilers to the limits and I hit probably a few compiler bugs in the meantime.

From the GCC Docs, [[maybe_unused]] is implemented since version 7, but if used this way:

struct foo {
    foo([[maybe_unused]] bool thing1)
    {
    }
};

I hit this specific error:

<source>:2:9: error: expected unqualified-id before '[' token
     foo([[maybe_unused]] bool thing1)
         ^
<source>:2:9: error: expected ')' before '[' token
     foo([[maybe_unused]] bool thing1)
        ~^
         )
Compiler returned: 1

Now, I know too little about C++17 to know if this error is correct, I know that clang 6 compiles that part fine (and fails somewhere else).

So, who's right, clang or gcc? (flags are -std=gnu++17 for both clang and gcc, generated by CMake)

回答1:

This is a known bug in g++: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81429 G++ doesn't parse correctly [[maybe_unused]] attribute for first argument of the constructor.



标签: c++ c++17