Created
November 11, 2020 20:49
-
-
Save vladiant/27ab1ec1822ee8c13057510c70ce27f5 to your computer and use it in GitHub Desktop.
Overload operator << for C style arrays
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 <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; | |
} | |
} |
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 <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