Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[[nodiscard]] for constructors #1495

Merged
merged 5 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stl/inc/atomic
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ inline void _Atomic_lock_release(_Smtx_t* _Spinlock) noexcept {
}

template <class _Spinlock_t>
class _Atomic_lock_guard {
class _NODISCARD _Atomic_lock_guard {
public:
explicit _Atomic_lock_guard(_Spinlock_t& _Spinlock_) noexcept : _Spinlock(_Spinlock_) {
_Atomic_lock_acquire(_Spinlock);
Expand Down
2 changes: 1 addition & 1 deletion stl/inc/condition_variable
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ _STL_DISABLE_CLANG_WARNINGS

_STD_BEGIN
template <class _Lock>
struct _Unlock_guard {
struct _NODISCARD _Unlock_guard {
explicit _Unlock_guard(_Lock& _Mtx_) : _Mtx(_Mtx_) {
_Mtx.unlock();
}
Expand Down
15 changes: 7 additions & 8 deletions stl/inc/memory
Original file line number Diff line number Diff line change
Expand Up @@ -2281,13 +2281,12 @@ void _Deallocate_flexible_array(_Refc* const _Ptr) noexcept {
}

template <class _NoThrowIt>
struct _Uninitialized_rev_destroying_backout {
struct _NODISCARD _Uninitialized_rev_destroying_backout {
// struct to undo partially constructed ranges in _Uninitialized_xxx algorithms
_NoThrowIt _First;
_NoThrowIt _Last;

explicit _Uninitialized_rev_destroying_backout(_NoThrowIt _Dest) noexcept
: _First(_Dest), _Last(_Dest) {} // TRANSITION, P1771R1 [[nodiscard]] For Constructors
explicit _Uninitialized_rev_destroying_backout(_NoThrowIt _Dest) noexcept : _First(_Dest), _Last(_Dest) {}

_Uninitialized_rev_destroying_backout(const _Uninitialized_rev_destroying_backout&) = delete;
_Uninitialized_rev_destroying_backout& operator=(const _Uninitialized_rev_destroying_backout&) = delete;
Expand Down Expand Up @@ -2329,7 +2328,7 @@ void _Reverse_destroy_multidimensional_n(_Ty* const _Arr, size_t _Size) noexcept
}

template <class _Ty>
struct _Reverse_destroy_multidimensional_n_guard {
struct _NODISCARD _Reverse_destroy_multidimensional_n_guard {
_Ty* _Target;
size_t _Index;

Expand Down Expand Up @@ -2646,15 +2645,15 @@ private:

#if _HAS_CXX20
template <class _Alloc>
class _Uninitialized_rev_destroying_backout_al {
class _NODISCARD _Uninitialized_rev_destroying_backout_al {
// class to undo partially constructed ranges in _Uninitialized_xxx_al algorithms

private:
using pointer = _Alloc_ptr_t<_Alloc>;

public:
_Uninitialized_rev_destroying_backout_al(pointer _Dest, _Alloc& _Al_) noexcept
: _First(_Dest), _Last(_Dest), _Al(_Al_) {} // TRANSITION, P1771R1 [[nodiscard]] For Constructors
: _First(_Dest), _Last(_Dest), _Al(_Al_) {}

_Uninitialized_rev_destroying_backout_al(const _Uninitialized_rev_destroying_backout_al&) = delete;
_Uninitialized_rev_destroying_backout_al& operator=(const _Uninitialized_rev_destroying_backout_al&) = delete;
Expand Down Expand Up @@ -2696,7 +2695,7 @@ void _Reverse_destroy_multidimensional_n_al(_Ty* const _Arr, size_t _Size, _Allo
}

template <class _Ty, class _Alloc>
struct _Reverse_destroy_multidimensional_n_al_guard {
struct _NODISCARD _Reverse_destroy_multidimensional_n_al_guard {
_Ty* _Target;
size_t _Index;
_Alloc& _Al;
Expand Down Expand Up @@ -2913,7 +2912,7 @@ _NODISCARD

#if _HAS_CXX20
template <class _Refc>
struct _Global_delete_guard {
struct _NODISCARD _Global_delete_guard {
_Refc* _Target;

~_Global_delete_guard() {
Expand Down
8 changes: 4 additions & 4 deletions stl/inc/mutex
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ _INLINE_VAR constexpr try_to_lock_t try_to_lock{};

// CLASS TEMPLATE unique_lock
template <class _Mutex>
class unique_lock { // whizzy class with destructor that unlocks mutex
class _NODISCARD unique_lock { // whizzy class with destructor that unlocks mutex
public:
using mutex_type = _Mutex;

Expand Down Expand Up @@ -421,7 +421,7 @@ void lock(_Lock0& _Lk0, _Lock1& _Lk1, _LockN&... _LkN) { // lock multiple locks,

// CLASS TEMPLATE lock_guard
template <class _Mutex>
class lock_guard { // class with destructor that unlocks a mutex
class _NODISCARD lock_guard { // class with destructor that unlocks a mutex
public:
using mutex_type = _Mutex;

Expand All @@ -445,7 +445,7 @@ private:
#if _HAS_CXX17
// CLASS TEMPLATE scoped_lock
template <class... _Mutexes>
class scoped_lock { // class with destructor that unlocks mutexes
class _NODISCARD scoped_lock { // class with destructor that unlocks mutexes
public:
explicit scoped_lock(_Mutexes&... _Mtxes) : _MyMutexes(_Mtxes...) { // construct and lock
_STD lock(_Mtxes...);
Expand All @@ -465,7 +465,7 @@ private:
};

template <class _Mutex>
class scoped_lock<_Mutex> {
class _NODISCARD scoped_lock<_Mutex> {
public:
using mutex_type = _Mutex;

Expand Down
2 changes: 1 addition & 1 deletion stl/inc/shared_mutex
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private:

// CLASS TEMPLATE shared_lock
template <class _Mutex>
class shared_lock { // shareable lock
class _NODISCARD shared_lock { // shareable lock
public:
using mutex_type = _Mutex;

Expand Down
4 changes: 2 additions & 2 deletions stl/inc/thread
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private:

public:
template <class _Fn, class... _Args, enable_if_t<!is_same_v<_Remove_cvref_t<_Fn>, thread>, int> = 0>
explicit thread(_Fn&& _Fx, _Args&&... _Ax) {
_NODISCARD_CTOR explicit thread(_Fn&& _Fx, _Args&&... _Ax) {
_Start(_STD forward<_Fn>(_Fx), _STD forward<_Args>(_Ax)...);
}

Expand Down Expand Up @@ -282,7 +282,7 @@ public:
jthread() noexcept : _Impl{}, _Ssource{nostopstate} {}

template <class _Fn, class... _Args, enable_if_t<!is_same_v<remove_cvref_t<_Fn>, jthread>, int> = 0>
explicit jthread(_Fn&& _Fx, _Args&&... _Ax) {
_NODISCARD_CTOR explicit jthread(_Fn&& _Fx, _Args&&... _Ax) {
if constexpr (is_invocable_v<decay_t<_Fn>, stop_token, decay_t<_Args>...>) {
_Impl._Start(_STD forward<_Fn>(_Fx), _Ssource.get_token(), _STD forward<_Args>(_Ax)...);
} else {
Expand Down
2 changes: 1 addition & 1 deletion stl/inc/xhash
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ private:
_Pocma(_Vec._Mypair._Get_first(), _Right._Vec._Mypair._Get_first());
}

struct _Clear_guard {
struct _NODISCARD _Clear_guard {
_Hash* _Target;

explicit _Clear_guard(_Hash* const _Target_) : _Target(_Target_) {}
Expand Down
4 changes: 2 additions & 2 deletions stl/inc/xlocale
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public:
facet& __CLR_OR_THIS_CALL operator=(const facet&) = delete;
};

struct _Facet_guard {
struct _NODISCARD _Facet_guard {
facet* _Target;
~_Facet_guard() {
if (_Target) {
Expand Down Expand Up @@ -1377,7 +1377,7 @@ private:

#if defined(__cpp_char8_t) && !defined(_M_CEE_PURE)
template <class _From, class _To>
struct _Codecvt_guard {
struct _NODISCARD _Codecvt_guard {
const _From* const& _First1;
const _From*& _Mid1;
_To* const& _First2;
Expand Down
12 changes: 7 additions & 5 deletions stl/inc/xmemory
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ _STL_DISABLE_CLANG_WARNINGS
_STD_BEGIN
// STRUCT TEMPLATE _Tidy_guard
template <class _Ty>
struct _Tidy_guard { // class with destructor that calls _Tidy
struct _NODISCARD _Tidy_guard { // class with destructor that calls _Tidy
_Ty* _Target;
~_Tidy_guard() {
if (_Target) {
Expand All @@ -36,7 +36,7 @@ struct _Tidy_guard { // class with destructor that calls _Tidy

// STRUCT TEMPLATE _Tidy_deallocate_guard
template <class _Ty>
struct _Tidy_deallocate_guard { // class with destructor that calls _Tidy_deallocate
struct _NODISCARD _Tidy_deallocate_guard { // class with destructor that calls _Tidy_deallocate
_Ty* _Target;
~_Tidy_deallocate_guard() {
if (_Target) {
Expand Down Expand Up @@ -226,7 +226,7 @@ void _Deallocate(void* _Ptr, size_t _Bytes) noexcept {
// FUNCTION TEMPLATE _Global_new
template <class _Ty, class... _Types>
_Ty* _Global_new(_Types&&... _Args) { // acts as "new" while disallowing user overload selection
struct _Guard_type {
struct _NODISCARD _Guard_type {
void* _Result;
~_Guard_type() {
if (_Result) {
Expand Down Expand Up @@ -1415,7 +1415,8 @@ void _Return_temporary_buffer(_Ty* const _Pbuf) noexcept {

// STRUCT TEMPLATE _Uninitialized_backout
template <class _NoThrowFwdIt>
struct _Uninitialized_backout { // struct to undo partially constructed ranges in _Uninitialized_xxx algorithms
struct _NODISCARD _Uninitialized_backout {
// struct to undo partially constructed ranges in _Uninitialized_xxx algorithms
_NoThrowFwdIt _First;
_NoThrowFwdIt _Last;

Expand Down Expand Up @@ -1513,7 +1514,8 @@ _NoThrowFwdIt _Uninitialized_move_unchecked(_InIt _First, const _InIt _Last, _No

// STRUCT TEMPLATE _Uninitialized_backout_al
template <class _Alloc>
class _Uninitialized_backout_al { // struct to undo partially constructed ranges in _Uninitialized_xxx_al algorithms
class _NODISCARD _Uninitialized_backout_al {
// struct to undo partially constructed ranges in _Uninitialized_xxx_al algorithms
using pointer = _Alloc_ptr_t<_Alloc>;

public:
Expand Down
8 changes: 8 additions & 0 deletions stl/inc/yvals_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@
// _HAS_NODISCARD (in vcruntime.h) controls:
// [[nodiscard]] attributes on STL functions

#ifndef __has_cpp_attribute
#define _NODISCARD_CTOR
#elif __has_cpp_attribute(nodiscard) >= 201907L
#define _NODISCARD_CTOR _NODISCARD
#else
#define _NODISCARD_CTOR
#endif

// Determine if we should use [[msvc::known_semantics]] to communicate to the compiler
// that certain type trait specializations have the standard-mandated semantics
#ifndef __has_cpp_attribute
Expand Down
4 changes: 2 additions & 2 deletions stl/src/atomic_wait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace {
CONDITION_VARIABLE _Condition;
};

struct _Guarded_wait_context : _Wait_context {
struct _NODISCARD _Guarded_wait_context : _Wait_context {
_Guarded_wait_context(const void* _Storage_, _Wait_context* const _Head) noexcept
: _Wait_context{_Storage_, _Head, _Head->_Prev, CONDITION_VARIABLE_INIT} {
_Prev->_Next = this;
Expand All @@ -44,7 +44,7 @@ namespace {
_Guarded_wait_context& operator=(const _Guarded_wait_context&) = delete;
};

class _SrwLock_guard {
class _NODISCARD _SrwLock_guard {
public:
explicit _SrwLock_guard(SRWLOCK& _Locked_) noexcept : _Locked(&_Locked_) {
AcquireSRWLockExclusive(_Locked);
Expand Down