Rewrite using operator overloading
This commit is contained in:
parent
9a450cca8a
commit
2afb334484
1 changed files with 9 additions and 8 deletions
|
@ -6,23 +6,24 @@ class channel {
|
||||||
private:
|
private:
|
||||||
std::any val;
|
std::any val;
|
||||||
public:
|
public:
|
||||||
void write(auto v) {
|
void operator<<(auto & v) {
|
||||||
val = v;
|
val = v;
|
||||||
}
|
}
|
||||||
auto read() {
|
void operator>>(auto & v) {
|
||||||
while (!val.has_value());
|
while (!val.has_value());
|
||||||
auto ret = val;
|
v = std::any_cast<decltype(v)>(val);
|
||||||
val.reset();
|
val.reset();
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
channel c;
|
channel c;
|
||||||
int x = 1;
|
int x = 1;
|
||||||
c.write(x);
|
c << x;
|
||||||
std::cout << std::any_cast<int>(c.read()) << std::endl;
|
|
||||||
std::string y = "Hello world";
|
std::string y = "Hello world";
|
||||||
c.write(y);
|
c >> x;
|
||||||
std::cout << std::any_cast<std::string>(c.read()) << std::endl;
|
c << y;
|
||||||
|
std::cout << x << std::endl;
|
||||||
|
c >> y;
|
||||||
|
std::cout << y << std::endl;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue