Json: [Clang] warning: use of the 'nodiscard' attribute is a C++17 extension [-Wc++17-extensions]

Created on 20 Mar 2019  路  4Comments  路  Source: nlohmann/json

  • What is the issue you have?

When compiling on Clang (tested 3.9.1 - 7.0.0) with the following compiler flags:
-Wpedantic -std=c++11

Clang displays warnings about:
warning: use of the 'nodiscard' attribute is a C++17 extension [-Wc++17-extensions]

Full log:

json.hpp:13087:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
json.hpp:14262:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
json.hpp:14306:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
json.hpp:18858:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
json.hpp:19627:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
json.hpp:19643:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
json.hpp:19736:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
json.hpp:19752:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
json.hpp:19824:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
json.hpp:19840:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
json.hpp:19911:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
json.hpp:19927:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
json.hpp:20516:5: error: use of the 'nodiscard' attribute is a C++17 extension [-Werror,-Wc++17-extensions]
    JSON_NODISCARD
    ^
json.hpp:496:34: note: expanded from macro 'JSON_NODISCARD'
        #define JSON_NODISCARD [[nodiscard]]
                                 ^
13 errors generated.
  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

Compile json.hpp (3.6.1) using Clang (tested 3.9.1 - 7.0.0) with the following compiler flags:
-Wpedantic -std=c++11

  • What is the expected behavior?

Compilation succeeds without error / warning?

  • And what is the actual behavior instead?

See above.

Clang (tested 3.9.1 - 7.0.0)

  • Did you use a released version of the library or the version from the develop branch?

Release 3.6.1

confirmed bug release item further change proposed fix

Most helpful comment

The downside of the first option seems more acceptable to me in the long run.

If the first option is taken, the clang users with -std=c++11/c++14 cannot enjoy warnings from [[nodiscard]]. It should be fine because nodiscard is supposed to be available since c++17. Users have an option to use -std=c++17 if they really want it.

If the second option is taken, the developers of this library cannot enjoy warnings from -Wc++17-extensions. It potentially makes the development process more error prone.

    #if __has_cpp_attribute(nodiscard)
        #if defined(__clang__) && !defined(JSON_HAS_CPP_17) // issue #1535
            #define JSON_NODISCARD
        #else
            #define JSON_NODISCARD [[nodiscard]]
        #endif

All 4 comments

The library checks whether the nodiscard attribute is available. Apparently, Clang reports it being available in C++11 mode, but then -Wc++17-extensions warns that it is a C++17 feature. This seems odd.

A solution could be to make the check more restrictive and switch off nodiscard unless C++17 is detected. I don't really like this. Alternatively, we could suppress the warning in the library. This is also not nice... Any opinions?

A solution could be to make the check more restrictive and switch off nodiscard unless C++17 is detected. I don't really like this.

Me neither.

Alternatively, we could suppress the warning in the library. This is also not nice... Any opinions?

At the moment, I'm leaning towards this second option, but I'm eager to hear what others think.

As a note, using:

#pragma clang diagnostic ignored "-Wc++1z-extensions"

Will cover earlier Clang versions that only support -Wc++1z-extensions, and is an alias for -Wc++17-extensions on newer Clang versions.

The downside of the first option seems more acceptable to me in the long run.

If the first option is taken, the clang users with -std=c++11/c++14 cannot enjoy warnings from [[nodiscard]]. It should be fine because nodiscard is supposed to be available since c++17. Users have an option to use -std=c++17 if they really want it.

If the second option is taken, the developers of this library cannot enjoy warnings from -Wc++17-extensions. It potentially makes the development process more error prone.

    #if __has_cpp_attribute(nodiscard)
        #if defined(__clang__) && !defined(JSON_HAS_CPP_17) // issue #1535
            #define JSON_NODISCARD
        #else
            #define JSON_NODISCARD [[nodiscard]]
        #endif

馃敄 release item in #1551.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

koemeet picture koemeet  路  4Comments

MariaRamos89 picture MariaRamos89  路  4Comments

bassosimone picture bassosimone  路  3Comments

zkelo picture zkelo  路  3Comments

alienzj picture alienzj  路  4Comments