Created
February 13, 2023 10:19
-
-
Save airglow923/7f18b5d71cc7804f80d39d5de467edb8 to your computer and use it in GitHub Desktop.
Print with fold expression separated by delimiter
This file contains hidden or 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 <ostream> | |
#include <string> | |
#include <type_traits> | |
#include <utility> | |
template <typename CharT, typename Arg> | |
auto | |
Print(std::ostream &os, [[maybe_unused]] CharT delimiter, const Arg &arg) | |
-> std::ostream & { | |
static_assert( | |
std::is_same_v<CharT, typename std::char_traits<CharT>::char_type>, | |
"'CharT' must be a valid character type."); | |
os << arg; | |
return os; | |
} | |
template <typename CharT, typename Arg, typename... Args> | |
auto | |
Print(std::ostream &os, CharT delimiter, const Arg &arg, Args &&...args) | |
-> std::ostream & { | |
static_assert( | |
std::is_same_v<CharT, typename std::char_traits<CharT>::char_type>, | |
"'CharT' must be a valid character type."); | |
Print(os, delimiter, arg) << delimiter; | |
return Print(os, delimiter, std::forward<Args>(args)...); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment