Skip to content

Commit 66cbe3e

Browse files
authored
Merge pull request #256 from elbeno/delete-has-a-reason
✨ Add reason to `= delete` where available
2 parents 73e1d48 + 2e619de commit 66cbe3e

File tree

4 files changed

+75
-34
lines changed

4 files changed

+75
-34
lines changed

include/stdx/compiler.hpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,26 @@
6767
#endif
6868

6969
#define STDX_DO_PRAGMA(X) _Pragma(#X)
70-
#ifdef __clang__
70+
#if defined(__clang__)
7171
#define STDX_PRAGMA(X) STDX_DO_PRAGMA(clang X)
7272
#else
7373
#define STDX_PRAGMA(X) STDX_DO_PRAGMA(GCC X)
7474
#endif
7575

76+
#ifndef STDX_DELETED
77+
#if __cpp_deleted_function >= 202403L
78+
#if defined(__clang__)
79+
#define STDX_DELETED(R) \
80+
STDX_PRAGMA(diagnostic push) \
81+
STDX_PRAGMA(diagnostic ignored "-Wunknown-warning-option") \
82+
STDX_PRAGMA(diagnostic ignored "-Wc++26-extensions") = \
83+
delete (R)STDX_PRAGMA(diagnostic pop)
84+
#else
85+
#define STDX_DELETED(R) = delete (R)
86+
#endif
87+
#else
88+
#define STDX_DELETED(R) = delete
89+
#endif
90+
#endif
91+
7692
// NOLINTEND(cppcoreguidelines-macro-usage)

include/stdx/rollover.hpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <stdx/compiler.hpp>
34
#include <stdx/concepts.hpp>
45

56
#include <type_traits>
@@ -77,10 +78,26 @@ template <typename T> struct rollover_t {
7778
return not(lhs == rhs);
7879
}
7980

80-
friend constexpr auto operator<(rollover_t, rollover_t) -> bool = delete;
81-
friend constexpr auto operator<=(rollover_t, rollover_t) -> bool = delete;
82-
friend constexpr auto operator>(rollover_t, rollover_t) -> bool = delete;
83-
friend constexpr auto operator>=(rollover_t, rollover_t) -> bool = delete;
81+
friend constexpr auto operator<(rollover_t, rollover_t)
82+
-> bool STDX_DELETED(
83+
"Comparison operators on rollover_t are deleted because "
84+
"they are non-transitive. If you know your data is safe, "
85+
"you can use cmp_less(rollover_t, rollover_t).");
86+
friend constexpr auto operator<=(rollover_t, rollover_t)
87+
-> bool STDX_DELETED(
88+
"Comparison operators on rollover_t are deleted because "
89+
"they are non-transitive. If you know your data is safe, "
90+
"you can use cmp_less(rollover_t, rollover_t).");
91+
friend constexpr auto operator>(rollover_t, rollover_t)
92+
-> bool STDX_DELETED(
93+
"Comparison operators on rollover_t are deleted because "
94+
"they are non-transitive. If you know your data is safe, "
95+
"you can use cmp_less(rollover_t, rollover_t).");
96+
friend constexpr auto operator>=(rollover_t, rollover_t)
97+
-> bool STDX_DELETED(
98+
"Comparison operators on rollover_t are deleted because "
99+
"they are non-transitive. If you know your data is safe, "
100+
"you can use cmp_less(rollover_t, rollover_t).");
84101

85102
[[nodiscard]] friend constexpr auto cmp_less(rollover_t lhs, rollover_t rhs)
86103
-> bool {

test/fail/CMakeLists.txt

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,7 @@ add_fail_tests(
2727
template_for_each_not_list
2828
to_address_undefined_on_function)
2929

30-
function(add_formatted_error_tests)
31-
add_fail_tests(ct_check)
32-
add_fail_tests(static_assert)
33-
add_fail_tests(static_assert_format)
34-
endfunction()
35-
36-
function(add_26_formatted_error_tests)
37-
add_compile_fail_test("static_assert.cpp" NAME static_assert_26 LIBRARIES
38-
stdx)
39-
add_compile_fail_test("static_assert_format.cpp" NAME
40-
static_assert_format_26 LIBRARIES stdx)
41-
target_compile_features(EXPECT_FAIL.static_assert_26 PRIVATE cxx_std_26)
42-
target_compile_features(EXPECT_FAIL.static_assert_format_26
43-
PRIVATE cxx_std_26)
44-
endfunction()
45-
4630
if(${CMAKE_CXX_STANDARD} GREATER_EQUAL 20)
47-
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang"
48-
AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL 15)
49-
add_formatted_error_tests()
50-
endif()
51-
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" AND ${CMAKE_CXX_COMPILER_VERSION}
52-
VERSION_GREATER_EQUAL 13.2)
53-
add_formatted_error_tests()
54-
endif()
55-
56-
if("cxx_std_26" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
57-
add_26_formatted_error_tests()
58-
endif()
59-
6031
add_fail_tests(
6132
atomic_bool_dec
6233
ct_format_mismatch
@@ -69,3 +40,32 @@ if(${CMAKE_CXX_STANDARD} GREATER_EQUAL 20)
6940
tuple_spaceship_with_element
7041
tuple_type_not_found)
7142
endif()
43+
44+
function(add_test_by_compiler CPP_NAME CXX_VERSION COMPILER_ID COMPILER_VERSION)
45+
if("cxx_std_${CXX_VERSION}" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
46+
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "${COMPILER_ID}"
47+
AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL
48+
${COMPILER_VERSION})
49+
set(TEST_NAME "${CPP_NAME}_${COMPILER_ID}_${COMPILER_VERSION}")
50+
add_compile_fail_test("${CPP_NAME}.cpp" NAME ${TEST_NAME} LIBRARIES
51+
stdx)
52+
target_compile_features("EXPECT_FAIL.${TEST_NAME}"
53+
PRIVATE "cxx_std_${CXX_VERSION}")
54+
endif()
55+
endif()
56+
endfunction()
57+
58+
add_test_by_compiler(ct_check 20 Clang 15)
59+
add_test_by_compiler(ct_check 20 GNU 13.2)
60+
add_test_by_compiler(static_assert 20 Clang 15)
61+
add_test_by_compiler(static_assert 20 GNU 13.2)
62+
add_test_by_compiler(static_assert_format 20 Clang 15)
63+
add_test_by_compiler(static_assert_format 20 GNU 13.2)
64+
65+
add_test_by_compiler(static_assert 26 Clang 17)
66+
add_test_by_compiler(static_assert 26 GNU 14)
67+
add_test_by_compiler(static_assert_format 26 Clang 17)
68+
add_test_by_compiler(static_assert_format 26 GNU 14)
69+
70+
add_test_by_compiler(rollover_less_than_26 26 Clang 19)
71+
add_test_by_compiler(rollover_less_than_26 26 GNU 15)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdx/rollover.hpp>
2+
3+
// EXPECT: deleted because they are non-transitive
4+
5+
auto main() -> int {
6+
using X = stdx::rollover_t<unsigned int>;
7+
[[maybe_unused]] auto cmp = X{} < X{1u};
8+
}

0 commit comments

Comments
 (0)