Skip to content

Conversation

@StephanTLavavej
Copy link
Member

@StephanTLavavej StephanTLavavej commented Jan 31, 2026

This is a prelude to using [[nodiscard]] literally everywhere, which will be a huge mechanical change.

Commits

  • Simplify nodiscard macro definitions.
    • MSVC, Clang, and EDG always support the most advanced form. I'm removing the control macro _HAS_NODISCARD from <vcruntime.h>.
  • _NODISCARD_CTOR_MSG => _NODISCARD_MSG
  • _NODISCARD_MSG(_Msg) => [[nodiscard(_Msg)]]
  • Improve message wrapping.
  • Replace _NODISCARD_CTOR with slightly more informative messages.
  • Reported VSO-2696854 "C1XX should support [[fallthrough]] in C++14 mode".
    • Clang and EDG already do.
  • Replace _NODISCARD_CTOR with _NODISCARD.
    • If compiler warnings aren't sufficiently clear that they're complaining about unused temporary objects, the compiler should be improved.
  • _NODISCARD_CTOR_LOCK is now identical to _NODISCARD_LOCK.

__has_cpp_attribute(nodiscard) Investigation

Click to expand:
C:\Temp>type meow.cpp
#ifndef __has_cpp_attribute
static_assert(false, "#ifndef __has_cpp_attribute, this is not C++.");
#elif __has_cpp_attribute(nodiscard) >= 201907L
static_assert(false, "__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully available.");
#elif __has_cpp_attribute(nodiscard) >= 201603L
static_assert(false, "__has_cpp_attribute(nodiscard) >= 201603L, only nodiscard-msg available.");
#else
static_assert(false, "C++ but really old, nodiscard-msg/ctor unavailable.");
#endif
C:\Temp>for %I in (/std:c++14 /std:c++17 /std:c++20) do (cl /EHsc /nologo /W4 /c %I meow.cpp)

C:\Temp>(cl /EHsc /nologo /W4 /c /std:c++14 meow.cpp )
meow.cpp
meow.cpp(4): error C2338: static assertion failed: '__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully available.'

C:\Temp>(cl /EHsc /nologo /W4 /c /std:c++17 meow.cpp )
meow.cpp
meow.cpp(4): error C2338: static assertion failed: '__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully available.'

C:\Temp>(cl /EHsc /nologo /W4 /c /std:c++20 meow.cpp )
meow.cpp
meow.cpp(4): error C2338: static assertion failed: '__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully available.'

C:\Temp>for %I in (/std:c++14 /std:c++17 /std:c++20) do (clang-cl /EHsc /nologo /W4 /c %I meow.cpp)

C:\Temp>(clang-cl /EHsc /nologo /W4 /c /std:c++14 meow.cpp )
meow.cpp(4,15): error: static assertion failed: __has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully
      available.
    4 | static_assert(false, "__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully available.");
      |               ^~~~~
1 error generated.

C:\Temp>(clang-cl /EHsc /nologo /W4 /c /std:c++17 meow.cpp )
meow.cpp(4,15): error: static assertion failed: __has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully
      available.
    4 | static_assert(false, "__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully available.");
      |               ^~~~~
1 error generated.

C:\Temp>(clang-cl /EHsc /nologo /W4 /c /std:c++20 meow.cpp )
meow.cpp(4,15): error: static assertion failed: __has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully
      available.
    4 | static_assert(false, "__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully available.");
      |               ^~~~~
1 error generated.

C:\Temp>for %I in (/std:c++14 /std:c++17 /std:c++20) do (cl /EHsc /nologo /W4 /c /BE %I meow.cpp)

C:\Temp>(cl /EHsc /nologo /W4 /c /BE /std:c++14 meow.cpp )
meow.cpp
"meow.cpp", line 4: error: static assertion failed with
          "__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully
          available."
  static_assert(false, "__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully available.");
  ^


C:\Temp>(cl /EHsc /nologo /W4 /c /BE /std:c++17 meow.cpp )
meow.cpp
"meow.cpp", line 4: error: static assertion failed with
          "__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully
          available."
  static_assert(false, "__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully available.");
  ^


C:\Temp>(cl /EHsc /nologo /W4 /c /BE /std:c++20 meow.cpp )
meow.cpp
"meow.cpp", line 4: error: static assertion failed with
          "__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully
          available."
  static_assert(false, "__has_cpp_attribute(nodiscard) >= 201907L, nodiscard-msg/ctor fully available.");
  ^

@StephanTLavavej StephanTLavavej requested a review from a team as a code owner January 31, 2026 12:52
@StephanTLavavej StephanTLavavej added the enhancement Something can be improved label Jan 31, 2026
@github-project-automation github-project-automation bot moved this to Initial Review in STL Code Reviews Jan 31, 2026
@StephanTLavavej StephanTLavavej moved this from Initial Review to Final Review in STL Code Reviews Jan 31, 2026
@AlexGuteniev

This comment was marked as resolved.

@StephanTLavavej

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Something can be improved

Projects

Status: Final Review

Development

Successfully merging this pull request may close these issues.

2 participants