-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanything-string.cpp
46 lines (42 loc) · 1.06 KB
/
anything-string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <string_view>
#include <sstream>
using namespace std::literals;
class AnythingString {
std::string str;
public:
AnythingString() {}
AnythingString(std::string_view value) {
str = value;
std::cerr << "string_view specialization called\n";
}
template<typename T>
AnythingString(const T& value) {
std::ostringstream oss;
oss << value;
str = oss.str();
std::cerr << "generic conversion called\n";
}
template<typename T>
AnythingString& operator=(const T& value) {
str = AnythingString(value).get();
return *this;
}
template<typename T>
AnythingString& operator+=(const T& value) {
str += AnythingString(value).get();
return *this;
}
std::string_view get() const { return str; }
};
std::ostream& operator<<(std::ostream& os, const AnythingString& s) {
return os << s.get();
}
int main() {
AnythingString s = 1.23;
s += ' ';
s += 45;
s += " anything"sv;
std::cout << s << '\n';
}