Skip to content

Commit bb00ff6

Browse files
committed
fix ref wrapper
1 parent 3944cfd commit bb00ff6

File tree

4 files changed

+43
-23
lines changed

4 files changed

+43
-23
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ Here is a list of configurable macros, define them **before** including this hea
168168
* `#define DEBUG_SHOW_TIMESTAMP 1` - enable printing a timestamp for each line of debug output (e.g. "09:57:32")
169169
* `#define DEBUG_SHOW_TIMESTAMP 2` (default) - printing timestamp relative to program staring time rather than system real time
170170

171+
* `#define DEBUG_SHOW_THREAD_ID 0` (default) - do not print the thread id
172+
* `#define DEBUG_SHOW_THREAD_ID 1` - print the current thread id
173+
171174
* `#define DEBUG_SHOW_LOCATION 1` (default) - show source location mark before each line of the debug output (e.g. "file.cpp:233")
172175
* `#define DEBUG_SHOW_LOCATION 0` - do not show the location mark
173176

README.zh_CN.md

+7
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ your_file.cpp:233: assertion failed: 3 < 0
165165
* `#define DEBUG_STEPPING 1` - 启用单步调试,每次打印调试输出时暂停,手动按下回车键以继续
166166
* `#define DEBUG_STEPPING 2` - 启用单步调试,但改为触发一个'陷阱'中断,以便调试器捕获
167167

168+
* `#define DEBUG_SHOW_TIMESTAMP 0` - 不打印时间戳
169+
* `#define DEBUG_SHOW_TIMESTAMP 1` - 打印时间戳(例如"09:57:32")
170+
* `#define DEBUG_SHOW_TIMESTAMP 2` (默认) - 打印时间戳,但是相对于程序启动时间而不是系统实时时间
171+
172+
* `#define DEBUG_SHOW_THREAD_ID 0` (默认) - 不打印线程ID
173+
* `#define DEBUG_SHOW_THREAD_ID 1` - 打印当前线程ID
174+
168175
* `#define DEBUG_SHOW_LOCATION 1` (默认) - 在每一行调试输出前加上打印该信息的代码文件名和行号 (例如:file.cpp:233)
169176
* `#define DEBUG_SHOW_LOCATION 0` - 不显示代码文件名和行号
170177

debug.hpp

+29-17
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484
// `#define DEBUG_SHOW_TIMESTAMP 2` (default) - printing timestamp relative to
8585
// program staring time rather than system real time
8686
//
87+
// `#define DEBUG_SHOW_THREAD_ID 0` (default) - do not print the thread id
88+
// `#define DEBUG_SHOW_THREAD_ID 1` - print the current thread id
89+
//
8790
// `#define DEBUG_SHOW_LOCATION 1` (default) - show source location mark before
8891
// each line of the debug output (e.g. "file.cpp:233")
8992
// `#define DEBUG_SHOW_LOCATION 0` - do not show the location mark
@@ -298,7 +301,7 @@
298301
# include <unordered_map>
299302
# endif
300303
# ifndef DEBUG_SOURCE_LOCATION
301-
# if __cplusplus >= 202'002L
304+
# if __cplusplus >= 202002L
302305
# if defined(__has_include)
303306
# if __has_include(<source_location>)
304307
# include <source_location>
@@ -310,7 +313,7 @@
310313
# endif
311314
# endif
312315
# ifndef DEBUG_SOURCE_LOCATION
313-
# if __cplusplus >= 201'505L
316+
# if __cplusplus >= 201505L
314317
# if defined(__has_include)
315318
# if __has_include(<experimental/source_location>)
316319
# include <experimental/source_location>
@@ -352,14 +355,17 @@
352355
# elif DEBUG_SHOW_TIMESTAMP == 2
353356
# include <chrono>
354357
# endif
358+
# if DEBUG_SHOW_THREAD_ID
359+
# include <thread>
360+
# endif
355361
# if defined(__has_include)
356362
# if __has_include(<variant>)
357363
# include <variant>
358364
# endif
359365
# endif
360366
# ifndef DEBUG_STRING_VIEW
361367
# if defined(__has_include)
362-
# if __cplusplus >= 201'703L
368+
# if __cplusplus >= 201703L
363369
# if __has_include(<string_view>)
364370
# include <string_view>
365371
# if __cpp_lib_string_view
@@ -373,7 +379,7 @@
373379
# include <string>
374380
# define DEBUG_STRING_VIEW std::string
375381
# endif
376-
# if __cplusplus >= 202'002L
382+
# if __cplusplus >= 202002L
377383
# if defined(__has_cpp_attribute)
378384
# if __has_cpp_attribute(unlikely)
379385
# define DEBUG_UNLIKELY [[unlikely]]
@@ -462,7 +468,7 @@ struct DEBUG_NODISCARD debug {
462468
}
463469

464470
struct debug_special_void {
465-
char const (&repr())[5] const {
471+
char const (&repr() const)[5] {
466472
return "void";
467473
}
468474
};
@@ -722,15 +728,15 @@ struct DEBUG_NODISCARD debug {
722728
bool add_comma = false;
723729
std::apply(
724730
[&](auto &&...args) {
725-
(([&] {
726-
if (add_comma) {
727-
oss << DEBUG_TUPLE_COMMA;
728-
}
729-
add_comma = true;
730-
debug_format(oss, std::forward<decltype(args)>(args));
731-
}()),
732-
...);
733-
},
731+
(([&] {
732+
if (add_comma) {
733+
oss << DEBUG_TUPLE_COMMA;
734+
}
735+
add_comma = true;
736+
debug_format(oss, std::forward<decltype(args)>(args));
737+
}()),
738+
...);
739+
},
734740
t);
735741
oss << DEBUG_TUPLE_BRACE[1];
736742
} else if constexpr (std::is_enum<T>::value) {
@@ -829,6 +835,12 @@ struct DEBUG_NODISCARD debug {
829835
std::declval<T const &>().get()));
830836
DEBUG_COND(is_optional, (((void)*std::declval<T const &>(), (void)0),
831837
((void)(bool)std::declval<T const &>(), (void)0)));
838+
DEBUG_COND(
839+
reference_wrapper,
840+
(typename std::enable_if<
841+
std::is_same<typename T::type &,
842+
decltype(std::declval<T const &>().get())>::value,
843+
int>::type)0);
832844
# define DEBUG_CON(n, ...) \
833845
template <class T> \
834846
struct debug_cond_##n : debug_bool_constant<__VA_ARGS__> {};
@@ -840,9 +852,6 @@ struct DEBUG_NODISCARD debug {
840852
DEBUG_CON(error_code, std::is_same<T, std::errc>::value ||
841853
std::is_same<T, std::error_code>::value ||
842854
std::is_same<T, std::error_condition>::value);
843-
DEBUG_CON(reference_wrapper,
844-
std::is_same<typename T::type &,
845-
decltype(std::declval<T const &>().get())>::value);
846855
# if __cpp_char8_t
847856
DEBUG_CON(unicode_char, std::is_same<T, char8_t>::value ||
848857
std::is_same<T, char16_t>::value ||
@@ -1578,6 +1587,9 @@ struct DEBUG_NODISCARD debug {
15781587
oss << elapsed % 1000;
15791588
oss.flags(flags);
15801589
oss << ' ';
1590+
# endif
1591+
# if DEBUG_SHOW_THREAD_ID
1592+
oss << '[' << std::this_thread::get_id() << ']' << ' ';
15811593
# endif
15821594
char const *fn = loc.file_name();
15831595
for (char const *fp = fn; *fp; ++fp) {

test.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define DEBUG_SHOW_THREAD_ID 1
12
#include "debug.hpp"
23
#include <map>
34
#include <set>
@@ -50,13 +51,9 @@ struct Student4 {
5051
#endif
5152
};
5253

53-
template <class T>
54-
void func(T const &t) {
55-
static_assert(std::is_same<T, char[3]>::value, "T must be std::string");
56-
}
57-
5854
int main() {
59-
func("ki");
55+
debug(), 1;
56+
#if 0
6057
int i = 42;
6158
debug(), "the answer:", i;
6259
std::vector<int> a{1, 2, 3, 4, 5};
@@ -178,6 +175,7 @@ int main() {
178175
debug(), z14;
179176

180177
return 0;
178+
#endif
181179
}
182180

183181
/*

0 commit comments

Comments
 (0)