Last active
November 23, 2021 02:34
-
-
Save airglow923/006cabc5f8e13358fac95c8193001f6e to your computer and use it in GitHub Desktop.
SFINAE and concept comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <concepts> | |
#include <iostream> | |
#include <type_traits> | |
struct BothAssignable { | |
constexpr auto operator=(const BothAssignable&) noexcept -> BothAssignable& = default; | |
constexpr auto operator=(BothAssignable&&) noexcept -> BothAssignable& = default; | |
}; | |
struct CopyAssignable { | |
constexpr auto operator=(const CopyAssignable&) noexcept -> CopyAssignable& = default; | |
constexpr auto operator=(CopyAssignable&&) noexcept -> CopyAssignable& = delete; | |
}; | |
struct MoveAssignable { | |
constexpr auto operator=(const MoveAssignable&) noexcept -> MoveAssignable& = delete; | |
constexpr auto operator=(MoveAssignable&&) noexcept -> MoveAssignable& = default; | |
}; | |
template <typename T, | |
typename std::enable_if_t<!std::is_move_assignable_v<T> && | |
std::is_copy_assignable_v<T>, | |
bool> = true> | |
auto func_sfinae() -> void { | |
std::cout << "is_copy_assignable\n"; | |
} | |
template <typename T, | |
typename std::enable_if_t<std::is_move_assignable_v<T>, bool> = false> | |
auto func_sfinae() -> void { | |
std::cout << "is_move_assignable\n"; | |
} | |
template <typename T> | |
requires (!std::is_move_assignable_v<T> && std::is_copy_assignable_v<T>) | |
auto func_concept() -> void { | |
std::cout << "is_copy_assignable\n"; | |
} | |
template <typename T> | |
requires std::is_move_assignable_v<T> | |
auto func_concept() -> void { | |
std::cout << "is_move_assignable\n"; | |
} | |
auto main() -> int { | |
func_sfinae<BothAssignable>(); // is_move_assignable | |
func_sfinae<CopyAssignable>(); // is_copy_assignable | |
func_sfinae<MoveAssignable>(); // is_move_assignable | |
func_concept<BothAssignable>(); // is_move_assignable | |
func_concept<CopyAssignable>(); // is_copy_assignable | |
func_concept<MoveAssignable>(); // is_move_assignable | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment