Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-104161

[spike] Find out whether all supported compilers support [[nodiscard]] on ctor, add macro if not

XMLWordPrintable

    • Icon: Task Task
    • Resolution: Fixed
    • Icon: P2: Important P2: Important
    • 6.6.0
    • None
    • Core: Other
    • None
    • 3
    • 959800f6d (dev)
    • Foundations Sprint 79, Foundations Sprint 80, Foundation Sprint 81

      We'd like to apply [[nodiscard]] to our RAII class' ctors (cf. epic for why), and we need to find out whether we need a macro for that in qcompilerdetection.h.

      For compilers that don't, check whether they act on __attribute__((__warn_unused_result__)) instead.

      Test case:

      QMutex mut;
      QMutexLocker<QMutex>{&mut}; // Clang already warns b/c of class-level [[nodiscard]], GCC does not
      

      Acceptance criteria:

      1. proove that all compilers syntactially support [[nodiscard]] on ctors
        • semantically, it's ok if they ignore it w/o warning
      2. or else add a macro Q_NODISCARD_CTOR to qcompilerdetection.h, expanding to
        1. [[nodiscard]]
        2. or __attribute__((__warn_unused_result__))
        3. or nothing, depending on what the particular compiler supports on ctors.
      3. add a test (can only be syntax-only, but add nonetheless)

      The 5min way to solve this is

      #if __has_cpp_attribute(nodiscard) >= 201907L
      # define Q_NODISCARD_CTOR [[nodiscard]]
      #else
      # define Q_NODISCARD_CTOR
      #endif
      

      but that would mean it's limited to C++20, while we'd really like this to be available in C++17, too.

        For Gerrit Dashboard: QTBUG-104161
        # Subject Branch Project Status CR V

            ivan.solovev Ivan Solovev
            mmutz Marc Mutz
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes