Advertisement
Zgragselus

Untitled

Jul 6th, 2023
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <concepts>
  2. #include <iostream>
  3. #include <type_traits>
  4.  
  5. #include <string>
  6.  
  7. template<typename T>
  8. concept ComplexSerializable = !std::is_arithmetic_v<T>;
  9.  
  10. void serialize(std::integral auto instance);
  11. void serialize(std::floating_point auto instance);
  12. void serialize(ComplexSerializable auto const& instance);
  13.  
  14. template<typename T>
  15. concept Serializable = requires (T t) {
  16.     {serialize(t)} -> std::same_as<void>;
  17. };
  18.  
  19. void serialize(std::integral auto instance) {
  20.     std::cout << "I: " << instance << std::endl;
  21. }
  22.  
  23. void serialize(std::floating_point auto instance) {
  24.     std::cout << "F: " << instance << std::endl;
  25. }
  26.  
  27. void serialize(ComplexSerializable auto const& instance) {
  28.     std::cout << "X: " << instance << std::endl;
  29. }
  30.  
  31. template<typename T>
  32. void TrySerialize(T value) {
  33.     if constexpr (Serializable<T>) {
  34.         serialize(value);
  35.     }
  36. }
  37.  
  38. int main() {
  39.     TrySerialize(4);
  40.     TrySerialize(2.0f);
  41.     TrySerialize(std::string("hello world"));
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement