Skip to content

Commit

Permalink
Add support for std::variant<..>
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Jul 7, 2019
1 parent 3dd500a commit 0e76f6a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
40 changes: 25 additions & 15 deletions dbg.h
Expand Up @@ -48,6 +48,7 @@ License (MIT):

#if __cplusplus >= 201703L
#include <optional>
#include <variant>
#endif

namespace dbg_macro {
Expand Down Expand Up @@ -328,21 +329,6 @@ bool pretty_print(std::ostream& stream, const std::tuple<>& value) {
return true;
}

#if __cplusplus >= 201703L

template <typename T>
bool pretty_print(std::ostream& stream, const std::optional<T>& value) {
if (value) {
stream << '{' << *value << '}';
} else {
stream << "nullopt";
}

return true;
}

#endif // __cplusplus >= 201703L

template <typename Container>
typename std::enable_if<detail::has_begin_end_size<const Container&>::value,
bool>::type
Expand Down Expand Up @@ -384,6 +370,30 @@ inline bool pretty_print(std::ostream& stream, const std::string& value) {
return true;
}

#if __cplusplus >= 201703L

template <typename T>
bool pretty_print(std::ostream& stream, const std::optional<T>& value) {
if (value) {
stream << '{' << *value << '}';
} else {
stream << "nullopt";
}

return true;
}

This comment has been minimized.

Copy link
@hmenke

hmenke Jul 9, 2019

Contributor
template <typename T>
bool pretty_print(std::ostream& stream, const std::optional<T>& value) {
    stream << value.value_or("nullopt");
}

This doesn't add curly braces around the value but I don't see why they are necessary here.

This comment has been minimized.

Copy link
@hmenke

hmenke Jul 9, 2019

Contributor

Sorry, this doesn't work. The argument of value_or has to be convertible to T.

This comment has been minimized.

Copy link
@sharkdp

sharkdp Jul 9, 2019

Author Owner

Speaking of ... we should actually call pretty_print on *value here.


template <typename... Ts>
bool pretty_print(std::ostream& stream, const std::variant<Ts...>& value) {
stream << "{";
std::visit([&stream](auto&& arg) { pretty_print(stream, arg); }, value);
stream << "}";

return true;
}

#endif // __cplusplus >= 201703L

class DebugOutput {
public:
DebugOutput(const char* filepath,
Expand Down
10 changes: 10 additions & 0 deletions tests/tests.cpp
Expand Up @@ -7,6 +7,11 @@
#include <tuple>
#include <vector>

#if __cplusplus >= 201703L
#include <optional>
#include <variant>
#endif

#include <dbg.h>

template <typename L, typename R>
Expand Down Expand Up @@ -197,6 +202,11 @@ int main() {
assert_eq(pretty_print(std::make_optional<int>(42)), "{42}");
std::optional<int> empty_optional;
assert_eq(pretty_print(empty_optional), "nullopt");

std::variant<int, std::string> dummy_variant = "test";
assert_eq(pretty_print(dummy_variant), "{\"test\"}");
dummy_variant = 42;
assert_eq(pretty_print(dummy_variant), "{42}");
#endif

dbg("====== type_name<T>() tests");
Expand Down

0 comments on commit 0e76f6a

Please sign in to comment.