Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <concepts>
- #include <iostream>
- #include <type_traits>
- #include <string>
- template<typename T>
- concept ComplexSerializable = !std::is_arithmetic_v<T>;
- void serialize(std::integral auto instance);
- void serialize(std::floating_point auto instance);
- void serialize(ComplexSerializable auto const& instance);
- template<typename T>
- concept Serializable = requires (T t) {
- {serialize(t)} -> std::same_as<void>;
- };
- void serialize(std::integral auto instance) {
- std::cout << "I: " << instance << std::endl;
- }
- void serialize(std::floating_point auto instance) {
- std::cout << "F: " << instance << std::endl;
- }
- void serialize(ComplexSerializable auto const& instance) {
- std::cout << "X: " << instance << std::endl;
- }
- template<typename T>
- void TrySerialize(T value) {
- if constexpr (Serializable<T>) {
- serialize(value);
- }
- }
- int main() {
- TrySerialize(4);
- TrySerialize(2.0f);
- TrySerialize(std::string("hello world"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement