Skip to content

Instantly share code, notes, and snippets.

@airglow923
Created February 13, 2023 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save airglow923/7f18b5d71cc7804f80d39d5de467edb8 to your computer and use it in GitHub Desktop.
Save airglow923/7f18b5d71cc7804f80d39d5de467edb8 to your computer and use it in GitHub Desktop.
Print with fold expression separated by delimiter
#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