C++ Logo

std-discussion

Advanced search

std::istream and std::ostream support for std::pair

From: Ferhat Geçdoğan <ferhatgectao_at_[hidden]>
Date: Fri, 26 Jan 2024 01:20:09 +0300
let say we pair two types, T and U. both have overloading functions over
std::istream and/or std::ostream, or just primitive types.

code example:
```
#include <utility>
#include <iostream>
#include <string>

int main() {
  int x = 31;
  std::string y = "hello world";

  auto x = std::make_pair(x, y);
  std::cout << x << '\n'; // output looks like "(31, "hello world")"
}
```

there can be U is can't printable while T is can. then just use special
character like '_' would be great, code example:

```
#include <utility>
#include <iostream>
#include <string>

class example {
 int x;
public:
 example(int y) : x(y) {}

 // here, no overloads for operator<<, hence it's not printable as we pass
the class as argument.
};

int main() {
  int x = 31;
  example y(42);

  auto x = std::make_pair(x, y);
  std::cout << x << '\n'; // output looks like "(31, _)"
}
```
it seems that like c++23 comes with std::formatter support for std::pair
and std::tuple, which is needed as well as std::ostream one i think.

Received on 2024-01-25 22:20:23