Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <concepts>
- #include <type_traits>
- #include <format>
- template <typename T>
- using base_type = std::remove_cvref_t<T>;
- // clang-format off
- template <typename T>
- concept is_char_type = (
- std::is_same_v<base_type<T>, const char *> || std::is_same_v<base_type<T>, char *> ||
- std::is_same_v<base_type<T>, const char8_t*> || std::is_same_v<base_type<T>, char8_t*> ||
- std::is_same_v<base_type<T>, const char16_t*> || std::is_same_v<base_type<T>, char16_t*> ||
- std::is_same_v<base_type<T>, const char32_t*> || std::is_same_v<base_type<T>, char32_t*> ||
- std::is_same_v<base_type<T>, const wchar_t*> || std::is_same_v<base_type<T>, wchar_t*>
- );
- // clang-format on
- template <typename T>
- auto
- quit_helper (const T &_val)
- {
- if constexpr (std::is_pointer_v<T> && !is_char_type<T>)
- return static_cast<void *const> (_val);
- else
- return _val;
- }
- template <typename T>
- auto
- quit_helper (T &_val)
- {
- if constexpr (std::is_pointer_v<T> && !is_char_type<T>)
- return static_cast<const void *> (_val);
- else
- return _val;
- }
- /**
- * @brief prints message according to provided arguments and calls
- * std::exit(1), terminating the application
- * @note Accepts stdlib string types
- * @tparam Args Packed types
- * @param fmt char type specifying how to format
- * @param args packed parameters to format according @p fmt
- */
- template <typename... Args>
- void
- quit (std::string &&fmt, Args &&...args)
- {
- auto m = std::vformat (
- std::string_view{ fmt },
- std::make_format_args (quit_helper (std::forward<Args> (args))...));
- std::cout << m << "\n";
- std::exit (1);
- }
- template <typename... Args>
- void
- quit (const char *fmt, Args &&...args)
- {
- quit (std::string{ fmt }, std::forward<Args> (args)...);
- }
- struct node {
- int fake;
- };
- struct game_object {
- float x;
- float y;
- };
- int main() {
- int a = 256;
- bool b = false;
- node* nptr = nullptr;
- game_object* nptr2 = new game_object();
- quit("Hello world, Int: {}, bool: {}, Arbritrary Ptr1: {} Abritrary Ptr2: {}", a, b, nptr, nptr2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement