Skip to content

Instantly share code, notes, and snippets.

@vladiant
Created November 11, 2020 20:49
Show Gist options
  • Save vladiant/27ab1ec1822ee8c13057510c70ce27f5 to your computer and use it in GitHub Desktop.
Save vladiant/27ab1ec1822ee8c13057510c70ce27f5 to your computer and use it in GitHub Desktop.
Overload operator << for C style arrays
#include <iostream>
#include <iomanip>
#include <sstream>
template<typename T, size_t N,
std::enable_if_t<!(std::is_same<T, char>::value||std::is_same<T, unsigned char>::value), bool> = true>
std::istream& operator>>(std::istream& in, T(&arr)[N]) {
for(auto& a: arr) {
in >> a;
}
return in;
}
int main()
{
std::string input = "1 2 3 4 5 6";
std::istringstream stream(input);
const int MAX = 6;
unsigned char cstr[MAX];
stream >> cstr;
for(auto a: cstr) {
std::cout << a << std::endl;
}
}
#include <iostream>
#include <iomanip>
#include <sstream>
std::istream& operator>>(std::istream& in, volatile char& a) {
char b;
in >> b;
a = b;
return in;
}
template<typename T, size_t N,
std::enable_if_t<!(std::is_same<T, char>::value||std::is_same<T, unsigned char>::value), bool> = true>
std::istream& operator>>(std::istream& in, T(&arr)[N]) {
for(auto& a: arr) {
in >> a;
}
return in;
}
int main()
{
std::string input = "1 2 3 4 5 6";
std::istringstream stream(input);
const int MAX = 6;
volatile char cstr[MAX];
stream >> cstr;
for(auto a: cstr) {
std::cout << a << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment