pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post b2de5f by Anonymous on Fri 27th Mar 2026 13:21
original post 2bd4db5 by Anonymous on Wed 18th Mar 2026 19:25
Download diff
Show old version | new version | both versions

    
11
/*
22
 * MIT License
33
 *
44
 * Copyright (c) 2026 Roland Mainz <roland.mainz@nrubsig.org>
55
 *
66
 * Permission is hereby granted, free of charge, to any person obtaining a copy
77
 * of this software and associated documentation files (the "Software"), to deal
88
 * in the Software without restriction, including without limitation the rights
99
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
 * copies of the Software, and to permit persons to whom the Software is
1111
 * furnished to do so, subject to the following conditions:
1212
 *
1313
 * The above copyright notice and this permission notice shall be included in all
1414
 * copies or substantial portions of the Software.
1515
 *
1616
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1919
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2020
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
 * SOFTWARE.
2323
 */
2424
2525
#ifndef ROV_INT_TEMPLATE_H
2626
#define ROV_INT_TEMPLATE_H 1
2727
2828
/*
2929
 * rov_int_template002.h - ISO C++ wrapper class for integer types to throw
3030
 * an exception for @(division|modulo)-by-0
3131
 *
32-
 * WARNING: Great PAIN has been invested to make sure this compiles with
32+
 * 1. WARNING: Great PAIN has been invested to make sure this compiles with
3333
 * any combination of { GNU g++, clang++, Visual Studio 2019 } * { C++11,
3434
 * C++14, C++17, C++20 }.
3535
 * Anyone who breaks this will be fed to one of { Ravenous Bugblatter Beast
3636
 * of Traal, Bel-Shamharoth } WITHOUT MERCY!
37+
 * 2. The code is explicitly MIT-licensed so we can pass it to compiler
3739
 */
3840
3941
/*
4042
 * Build config
4143
 */
4244
4345
/*
4446
 * DEBUG-only: Add random data around internal <T> variable to prevent the usage
4547
 * as raw object passed to |printf()|&co.
4648
 */
4749
// #ifndef NDEBUG
4850
#define ROV_INT_BUILD_TEST_RAW_VALUE_CLOAKING_PADDING 1
4951
// #endif /* NDEBUG */
5052
#define ROV_INT_BUILD_THROW_EXCEPTION_DIVMODULO_BY_ZERO 1
5153
5254
/*
5355
 * |template <typename T> class rov_int_template| - wrapper class for integers
5456
 * to throw an exception if @(division|modulo)-by-zero occurs
5557
 */
5658
5759
#include <cstdint>
5860
#include <type_traits>
5961
#include <utility>
62+
#include <stdexcept>
6063
6164
/* Does this C++ version have the C++ <=> starship operator ? */
6265
#if __cplusplus >= 202002L
6366
#include <compare>
6467
#define ROV_HAS_SPACESHIP 1
6568
#endif /* __cplusplus >= 202002L */
6669
6770
/* Do we need |constexpr| to avoid the wrath of the professor ? */
6871
#if __cplusplus >= 201402L
6972
#define ROV_CXX14_CONSTEXPR constexpr
7073
#else
7174
#define ROV_CXX14_CONSTEXPR
7275
#endif /* __cplusplus >= 201402L */
7376
7477
template <typename T>
7578
class rov_int_template {
7679
#if 1
7780
	/* Compile-time check to prevent abuse */
7881
	static_assert(std::is_integral<T>::value, "rov_int_template: Type must be an integer");
7982
#endif
8083
8184
protected:
8285
#ifdef ROV_INT_BUILD_TEST_RAW_VALUE_CLOAKING_PADDING
8386
	long debug_cloaking_pad0 = 0xDEADBEEF;
8487
#endif /* ROV_INT_BUILD_TEST_RAW_VALUE_CLOAKING_PADDING */
8588
	T value_;
8689
#ifdef ROV_INT_BUILD_TEST_RAW_VALUE_CLOAKING_PADDING
8790
	long debug_cloaking_pad1 = 0x12345678;
8891
#endif /* ROV_INT_BUILD_TEST_RAW_VALUE_CLOAKING_PADDING */
8992
9093
public:
9194
	/* FIXME: Perf issue ? */
92-
	constexpr rov_int_template() noexcept : value_(0) {
95+
	constexpr rov_int_template() : value_(0) {
9396
	}
9497
9598
	template <typename U, typename = typename std::enable_if<std::is_integral<U>::value>::type>
96-
	constexpr rov_int_template(U v) noexcept : value_(static_cast<T>(v)) {
99+
	constexpr rov_int_template(U v) : value_(static_cast<T>(v)) {
97100
	}
98101
99102
	template <typename U>
100-
	constexpr rov_int_template(const rov_int_template<U>& other) noexcept : value_(static_cast<T>(other.get())) {
103+
	constexpr rov_int_template(const rov_int_template<U>& other) : value_(static_cast<T>(other.get())) {
101104
	}
102105
103-
	constexpr T get() const noexcept {
106+
	constexpr T get() const {
104107
		return value_;
105108
	}
106109
107-
	constexpr operator T() const noexcept {
110+
	constexpr operator T() const {
108111
		return value_;
109112
	}
110113
111114
	template <typename U, typename = typename std::enable_if<std::is_integral<U>::value>::type>
112-
	ROV_CXX14_CONSTEXPR rov_int_template& operator=(U v) noexcept {
115+
	ROV_CXX14_CONSTEXPR rov_int_template& operator=(U v) {
113116
		value_ = static_cast<T>(v);
114117
		return *this;
115118
	}
116119
117120
	template <typename U>
118-
	ROV_CXX14_CONSTEXPR rov_int_template& operator=(const rov_int_template<U>& other) noexcept {
121+
	ROV_CXX14_CONSTEXPR rov_int_template& operator=(const rov_int_template<U>& other) {
119122
		value_ = static_cast<T>(other.get());
120123
		return *this;
121124
	}
122125
123-
	constexpr rov_int_template<decltype(+std::declval<T>())> operator+() const noexcept {
126+
	constexpr rov_int_template<decltype(+std::declval<T>())> operator+() const {
124127
		return rov_int_template<decltype(+std::declval<T>())>(+value_);
125128
	}
126129
127-
	constexpr rov_int_template<decltype(-std::declval<T>())> operator-() const noexcept {
130+
	constexpr rov_int_template<decltype(-std::declval<T>())> operator-() const {
128131
		return rov_int_template<decltype(-std::declval<T>())>(-value_);
129132
	}
130133
131-
	constexpr rov_int_template<decltype(~std::declval<T>())> operator~() const noexcept {
134+
	constexpr rov_int_template<decltype(~std::declval<T>())> operator~() const {
132135
		return rov_int_template<decltype(~std::declval<T>())>(~value_);
133136
	}
134137
135-
	constexpr bool operator!() const noexcept {
138+
	constexpr bool operator!() const {
136139
		return !value_;
137140
	}
138141
139-
	ROV_CXX14_CONSTEXPR rov_int_template& operator++() noexcept {
142+
	ROV_CXX14_CONSTEXPR rov_int_template& operator++() {
140143
		++value_;
141144
		return *this;
142145
	}
143-
	ROV_CXX14_CONSTEXPR rov_int_template operator++(int) noexcept {
146+
	ROV_CXX14_CONSTEXPR rov_int_template operator++(int) {
144147
		rov_int_template tmp(*this);
145148
		++value_;
146149
		return tmp;
147150
	}
148151
149-
	ROV_CXX14_CONSTEXPR rov_int_template& operator--() noexcept {
152+
	ROV_CXX14_CONSTEXPR rov_int_template& operator--() {
150153
		--value_;
151154
		return *this;
152155
	}
153-
	ROV_CXX14_CONSTEXPR rov_int_template operator--(int) noexcept {
156+
	ROV_CXX14_CONSTEXPR rov_int_template operator--(int) {
154157
		rov_int_template tmp(*this);
155158
		--value_;
156159
		return tmp;
157160
	}
158161
159162
#define ROV_MEMBER_BINOP(OP) \
160163
	template <typename U> \
161-
	constexpr rov_int_template<decltype(std::declval<T>() OP std::declval<U>())> operator OP(const rov_int_template<U>& rhs) const noexcept { \
164+
	constexpr rov_int_template<decltype(std::declval<T>() OP std::declval<U>())> operator OP(const rov_int_template<U>& rhs) const { \
162165
		return rov_int_template<decltype(std::declval<T>() OP std::declval<U>())>(value_ OP rhs.get()); \
163166
	} \
164167
	template <typename U, typename = typename std::enable_if<std::is_integral<U>::value>::type> \
165-
	constexpr rov_int_template<decltype(std::declval<T>() OP std::declval<U>())> operator OP(U rhs) const noexcept { \
168+
	constexpr rov_int_template<decltype(std::declval<T>() OP std::declval<U>())> operator OP(U rhs) const { \
166169
		return rov_int_template<decltype(std::declval<T>() OP std::declval<U>())>(value_ OP rhs); \
167170
	}
168171
169172
	ROV_MEMBER_BINOP(+)
170173
	ROV_MEMBER_BINOP(-)
171174
	ROV_MEMBER_BINOP(*)
172175
#ifdef ROV_INT_BUILD_THROW_EXCEPTION_DIVMODULO_BY_ZERO
173176
	/* division operator */
174177
	template <typename U>
175-
	constexpr rov_int_template<decltype(std::declval<T>() / std::declval<U>())> operator /(const rov_int_template<U>& rhs) const noexcept {
178+
	constexpr rov_int_template<decltype(std::declval<T>() / std::declval<U>())> operator /(const rov_int_template<U>& rhs) const {
176179
		if (rhs.get() == 0)
177-
			(void)puts("ERROR division by zero");
180+
			throw std::runtime_error("ERROR division by zero");
178181
		return rov_int_template<decltype(std::declval<T>() / std::declval<U>())>(value_ / rhs.get());
179182
	}
180183
181184
	template <typename U, typename = typename std::enable_if<std::is_integral<U>::value>::type>
182-
	constexpr rov_int_template<decltype(std::declval<T>() / std::declval<U>())> operator /(U rhs) const noexcept {
185+
	constexpr rov_int_template<decltype(std::declval<T>() / std::declval<U>())> operator /(U rhs) const {
183186
		if (rhs == 0)
184-
			(void)puts("ERROR division by zero");
187+
			throw std::runtime_error("ERROR division by zero");
185188
		return rov_int_template<decltype(std::declval<T>() / std::declval<U>())>(value_ / rhs);
186189
	}
187190
188191
	/* Modulo operator */
189192
	template <typename U>
190-
	constexpr rov_int_template<decltype(std::declval<T>() % std::declval<U>())> operator %(const rov_int_template<U>& rhs) const noexcept {
193+
	constexpr rov_int_template<decltype(std::declval<T>() % std::declval<U>())> operator %(const rov_int_template<U>& rhs) const {
191194
		if (rhs.get() == 0)
192-
			(void)puts("ERROR modulo by zero");
195+
			throw std::runtime_error("ERROR modulo by zero");
193196
		return rov_int_template<decltype(std::declval<T>() % std::declval<U>())>(value_ % rhs.get());
194197
	}
195198
	template <typename U, typename = typename std::enable_if<std::is_integral<U>::value>::type>
196-
	constexpr rov_int_template<decltype(std::declval<T>() % std::declval<U>())> operator %(U rhs) const noexcept {
199+
	constexpr rov_int_template<decltype(std::declval<T>() % std::declval<U>())> operator %(U rhs) const {
197200
		if (rhs == 0)
198-
			(void)puts("ERROR modulo by zero");
201+
			throw std::runtime_error("ERROR modulo by zero");
199202
		return rov_int_template<decltype(std::declval<T>() % std::declval<U>())>(value_ % rhs);
200203
	}
201204
#else
202205
	ROV_MEMBER_BINOP(/)
203206
	ROV_MEMBER_BINOP(%)
204207
#endif /* ROV_INT_BUILD_THROW_EXCEPTION_DIVMODULO_BY_ZERO */
205208
	ROV_MEMBER_BINOP(<<)
206209
	ROV_MEMBER_BINOP(>>)
207210
	ROV_MEMBER_BINOP(|)
208211
	ROV_MEMBER_BINOP(&)
209212
	ROV_MEMBER_BINOP(^)
210213
#undef ROV_MEMBER_BINOP
211214
212215
#define ROV_MEMBER_COMPOUND_OP(OP) \
213216
	template <typename U> \
214-
	ROV_CXX14_CONSTEXPR rov_int_template& operator OP(const rov_int_template<U>& rhs) noexcept { \
217+
	ROV_CXX14_CONSTEXPR rov_int_template& operator OP(const rov_int_template<U>& rhs) { \
215218
		value_ OP rhs.get(); \
216219
		return *this; \
217220
	} \
218221
	template <typename U, typename = typename std::enable_if<std::is_integral<U>::value>::type> \
219-
	ROV_CXX14_CONSTEXPR rov_int_template& operator OP(U rhs) noexcept { \
222+
	ROV_CXX14_CONSTEXPR rov_int_template& operator OP(U rhs) { \
220223
		value_ OP rhs; \
221224
		return *this; \
222225
	}
223226
224227
	ROV_MEMBER_COMPOUND_OP(+=)
225228
	ROV_MEMBER_COMPOUND_OP(-=)
226229
	ROV_MEMBER_COMPOUND_OP(*=)
227230
#ifdef ROV_INT_BUILD_THROW_EXCEPTION_DIVMODULO_BY_ZERO
228231
	template <typename U>
229-
	ROV_CXX14_CONSTEXPR rov_int_template& operator /=(const rov_int_template<U>& rhs) noexcept {
232+
	ROV_CXX14_CONSTEXPR rov_int_template& operator /=(const rov_int_template<U>& rhs) {
230233
		if (rhs.get() == 0)
231-
			(void)puts("ERROR division by zero");
234+
			throw std::runtime_error("ERROR division by zero");
232235
		value_ /= rhs.get();
233236
		return *this;
234237
	}
235238
	template <typename U, typename = typename std::enable_if<std::is_integral<U>::value>::type>
236-
	ROV_CXX14_CONSTEXPR rov_int_template& operator /=(U rhs) noexcept {
239+
	ROV_CXX14_CONSTEXPR rov_int_template& operator /=(U rhs) {
237240
		if (rhs == 0)
238-
			(void)puts("ERROR modulo by zero");
241+
			throw std::runtime_error("ERROR modulo by zero");
239242
		value_ /= rhs;
240243
		return *this;
241244
	}
242245
243246
	/* Modulo operator */
244247
	template <typename U>
245-
	ROV_CXX14_CONSTEXPR rov_int_template& operator %=(const rov_int_template<U>& rhs) noexcept {
248+
	ROV_CXX14_CONSTEXPR rov_int_template& operator %=(const rov_int_template<U>& rhs) {
246249
		if (rhs.get() == 0)
247-
			(void)puts("ERROR division by zero");
250+
			throw std::runtime_error("ERROR modulo by zero");
248251
		value_ %= rhs.get();
249252
		return *this;
250253
	}
251254
	template <typename U, typename = typename std::enable_if<std::is_integral<U>::value>::type>
252-
	ROV_CXX14_CONSTEXPR rov_int_template& operator %=(U rhs) noexcept {
255+
	ROV_CXX14_CONSTEXPR rov_int_template& operator %=(U rhs) {
253256
		if (rhs == 0)
254-
			(void)puts("ERROR modulo by zero");
257+
			throw std::runtime_error("ERROR modulo by zero");
255258
		value_ %= rhs;
256259
		return *this;
257260
	}
258261
#else
259262
	ROV_MEMBER_COMPOUND_OP(/=)
260263
	ROV_MEMBER_COMPOUND_OP(%=)
261264
#endif /* ROV_INT_BUILD_THROW_EXCEPTION_DIVMODULO_BY_ZERO */
262265
	ROV_MEMBER_COMPOUND_OP(<<=)
263266
	ROV_MEMBER_COMPOUND_OP(>>=)
264267
	ROV_MEMBER_COMPOUND_OP(|=)
265268
	ROV_MEMBER_COMPOUND_OP(&=)
266269
	ROV_MEMBER_COMPOUND_OP(^=)
267270
#undef ROV_MEMBER_COMPOUND_OP
268271
269272
#ifdef ROV_HAS_SPACESHIP
270273
	template <typename U>
271-
	constexpr auto operator<=>(const rov_int_template<U>& rhs) const noexcept {
274+
	constexpr auto operator<=>(const rov_int_template<U>& rhs) const {
272275
		return value_ <=> rhs.get();
273276
	}
274277
	template <typename U, typename = typename std::enable_if<std::is_integral<U>::value>::type>
275-
	constexpr auto operator<=>(U rhs) const noexcept {
278+
	constexpr auto operator<=>(U rhs) const {
276279
		return value_ <=> rhs;
277280
	}
278281
	template <typename U>
279-
	constexpr bool operator==(const rov_int_template<U>& rhs) const noexcept {
282+
	constexpr bool operator==(const rov_int_template<U>& rhs) const {
280283
		return value_ == rhs.get();
281284
	}
282285
	template <typename U, typename = typename std::enable_if<std::is_integral<U>::value>::type>
283-
	constexpr bool operator==(U rhs) const noexcept {
286+
	constexpr bool operator==(U rhs) const {
284287
		return value_ == rhs;
285288
	}
286289
#else
287290
#define ROV_COMP_OP(OP) \
288291
	template <typename U> \
289-
		constexpr bool operator OP(const rov_int_template<U>& rhs) const noexcept { return value_ OP rhs.get(); } \
292+
		constexpr bool operator OP(const rov_int_template<U>& rhs) const { return value_ OP rhs.get(); } \
290293
	template <typename U, typename = typename std::enable_if<std::is_integral<U>::value>::type> \
291-
		constexpr bool operator OP(U rhs) const noexcept { return value_ OP rhs; }
294+
		constexpr bool operator OP(U rhs) const { return value_ OP rhs; }
292295
293296
	ROV_COMP_OP(==)
294297
	ROV_COMP_OP(!=)
295298
	ROV_COMP_OP(<)
296299
	ROV_COMP_OP(<=)
297300
	ROV_COMP_OP(>)
298301
	ROV_COMP_OP(>=)
299302
300303
#undef ROV_COMP_OP
301304
#endif /* ROV_HAS_SPACESHIP */
302305
};
303306
304307
#define ROV_NONMEMBER_BINOP(OP) \
305308
	template <typename U, typename T, typename = typename std::enable_if<std::is_integral<U>::value>::type> \
306-
	constexpr rov_int_template<decltype(std::declval<U>() OP std::declval<T>())> operator OP(U lhs, const rov_int_template<T>& rhs) noexcept { \
309+
	constexpr rov_int_template<decltype(std::declval<U>() OP std::declval<T>())> operator OP(U lhs, const rov_int_template<T>& rhs) { \
307310
		return rov_int_template<decltype(std::declval<U>() OP std::declval<T>())>(lhs OP rhs.get()); \
308311
	}
309312
310313
	ROV_NONMEMBER_BINOP(+)
311314
	ROV_NONMEMBER_BINOP(-)
312315
	ROV_NONMEMBER_BINOP(*)
313316
#ifdef ROV_INT_BUILD_THROW_EXCEPTION_DIVMODULO_BY_ZERO
314317
	/* Division operator */
315318
	template <typename U, typename T, typename = typename std::enable_if<std::is_integral<U>::value>::type>
316-
	constexpr rov_int_template<decltype(std::declval<U>() / std::declval<T>())> operator /(U lhs, const rov_int_template<T>& rhs) noexcept {
319+
	constexpr rov_int_template<decltype(std::declval<U>() / std::declval<T>())> operator /(U lhs, const rov_int_template<T>& rhs) {
317320
		if (rhs.get() == 0)
318-
			(void)puts("ERROR division by zero");
321+
			throw std::runtime_error("ERROR division by zero");
319322
		return rov_int_template<decltype(std::declval<U>() / std::declval<T>())>(lhs / rhs.get());
320323
	}
321324
322325
	/* Modulo operator */
323326
	template <typename U, typename T, typename = typename std::enable_if<std::is_integral<U>::value>::type>
324-
	constexpr rov_int_template<decltype(std::declval<U>() % std::declval<T>())> operator %(U lhs, const rov_int_template<T>& rhs) noexcept {
327+
	constexpr rov_int_template<decltype(std::declval<U>() % std::declval<T>())> operator %(U lhs, const rov_int_template<T>& rhs) {
325328
		if (rhs.get() == 0)
326-
			(void)puts("ERROR modulo by zero");
329+
			throw std::runtime_error("ERROR modulo by zero");
327330
		return rov_int_template<decltype(std::declval<U>() % std::declval<T>())>(lhs % rhs.get());
328331
	}
329332
#else
330333
	ROV_NONMEMBER_BINOP(/)
331334
	ROV_NONMEMBER_BINOP(%)
332335
#endif /* ROV_INT_BUILD_THROW_EXCEPTION_DIVMODULO_BY_ZERO */
333336
	ROV_NONMEMBER_BINOP(<<)
334337
	ROV_NONMEMBER_BINOP(>>)
335338
	ROV_NONMEMBER_BINOP(|)
336339
	ROV_NONMEMBER_BINOP(&)
337340
	ROV_NONMEMBER_BINOP(^)
338341
339342
#undef ROV_NONMEMBER_BINOP
340343
341344
#ifndef ROV_HAS_SPACESHIP
342345
#define ROV_NONMEMBER_COMP_OP(OP) \
343346
	template <typename U, typename T, typename = typename std::enable_if<std::is_integral<U>::value>::type> \
344-
		constexpr bool operator OP(U lhs, const rov_int_template<T>& rhs) noexcept { return lhs OP rhs.get(); }
347+
		constexpr bool operator OP(U lhs, const rov_int_template<T>& rhs) { return lhs OP rhs.get(); }
345348
346349
	ROV_NONMEMBER_COMP_OP(==)
347350
	ROV_NONMEMBER_COMP_OP(!=)
348351
	ROV_NONMEMBER_COMP_OP(<)
349352
	ROV_NONMEMBER_COMP_OP(<=)
350353
	ROV_NONMEMBER_COMP_OP(>)
351354
	ROV_NONMEMBER_COMP_OP(>=)
352355
#undef ROV_NONMEMBER_COMP_OP
353356
#endif /* !ROV_HAS_SPACESHIP */
354357
355358
#endif /* !ROV_INT_TEMPLATE_H */
356359

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at